ASP.NET C# - Dropdown list by using User Control

2019-07-15 23:40发布

I am new to ASP.NET

Someone in this forum helped me how to get the dropdown list work wth user countrol and it is working.

In my user control file, VendorListControl.ascx, I have this code below. Please assume that the VendorListControl.ascx.cs works correctly, which is when I select a VendorName, it will fired "ddlVendor_SelectedIndexChanged" to refreshed the "ddlVendorBUList" dropdown list.

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="VendorListControl.ascx.cs" Inherits="MyNamespace.VendorListControl" %>
<asp:DropDownList runat="server" ID="ddlVendorList" onselectedindexchanged="ddlVendor_SelectedIndexChanged" AutoPostBack="True" /> 
<asp:DropDownList runat="server" ID="ddlVendorBUList" AutoPostBack="True" /> 
<asp:Label runat="server" ID="lblMessage" /> 

My VendorListControl.ascx.cs code:

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace MyNamespace
{
    public partial class VendorListControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillVendors();
            }
        }

        protected void ddlVendor_SelectedIndexChanged(object sender, EventArgs e)
        {
            int VendorID = Convert.ToInt32(ddlVendorList.SelectedValue.ToString());
            FillVendorBU(VendorID);
        }

        private void FillVendors()
        {
            string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(strConn);

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT VendorID, VendorName FROM MDF_Vendor";

            DataSet objDs = new DataSet();
            SqlDataAdapter dAdapter = new SqlDataAdapter();
            dAdapter.SelectCommand = cmd; ;
            conn.Open();
            dAdapter.Fill(objDs);
            conn.Close();

            if (objDs.Tables[0].Rows.Count > 0)
            {
                this.ddlVendorList.DataSource = objDs.Tables[0];
                this.ddlVendorList.DataTextField = "VendorName";
                this.ddlVendorList.DataValueField = "VendorID";
                this.ddlVendorList.DataBind();
                this.ddlVendorList.Items.Insert(0, "-- Select --");
            }
            else
            {
                this.lblMessage.Text = "No Vendor Found";
            }
        }

        private void FillVendorBU(int VendorID)
        {
            string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(strConn);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT VendorBUID, VendorBUName FROM dbo.MDF_VendorBU WHERE VendorID = @VendorID";
            cmd.Parameters.AddWithValue("@VendorID", VendorID);
            DataSet objDs = new DataSet();
            SqlDataAdapter dAdapter = new SqlDataAdapter();
            dAdapter.SelectCommand = cmd;
            con.Open();
            dAdapter.Fill(objDs);
            con.Close();
            if (objDs.Tables[0].Rows.Count > 0)
            {
                ddlVendorBUList.DataSource = objDs.Tables[0];
                ddlVendorBUList.DataTextField = "VendorBUName";
                ddlVendorBUList.DataValueField = "VendorBUID";
                ddlVendorBUList.DataBind();
                ddlVendorBUList.Items.Insert(0, "--Select--");
            }
            else
            {
                lblMessage.Text = "No states found";
            }
        }

    }
}

Next, in my CreateNewRecord.aspx page, I have this code to include both dropdown list from user control. And I can see the dropdown lists works properly.

<%@ Register TagPrefix="uc" TagName="VendorListControl" Src="Controls/VendorListControl.ascx" %>
// Some where in the form
<tr>
    <td class="right" width="20%">Vendor Name:</td>
       <td>
          <uc:VendorListControl runat="server" />
       </td>
</tr>

The problem is related to the ID of those two dropdown lists. When I attemp to do the insert record, it seems not to detect the ID for "ddlVendorList" and "ddlVendorBUList" come from user control ascx page. Error " The name 'ddlVendorList' does not exist in the current context"

ExecuteInsert(ddlVendorList.SelectedItem.Text,
  ddlVendorBUList.SelectedItem.Text, 
  MDFAmount.Text, 
  StartDate.Text,
  EndDate.Text,
  VendorQuarter.Text,
  MDFName.Text,
  MDFSummary.Text,
  Status.SelectedItem.Text,
  CreatedBy.Value
  );
Response.Write("<b>Record was successfully added!</b>");

I know I am new to ASP.NET, so please help.

2条回答
Anthone
2楼-- · 2019-07-16 00:18

You define public property who return SelectedItem.Text in your UserControl.

User Control (Ascx)

public string YourValue
{
  get
  {
    return ddlVendorList.SelectedItem.Text;
  }

}

Page (Aspx)

You can use your public property : YourValue.

ExecuteInsert(YourValue,
.......  );

Nota : if you wish set value, you define setter on your property

After your update add theses properties

public string YourDdlVendorListSelectedItemText
{
   get 
   { 
      return this.ddlVendorList.text; 
   }
}

public string YourDdlVendorBUListSelectedItemText
{
   get 
   { 
     return this.ddlVendorBUList.text; 
   }
}
查看更多
劫难
3楼-- · 2019-07-16 00:22

You can put two properties in your VendorListControl to get the ddlVendorList selectedItem text and the ddlVendorBUList selectedItem text.

In VendorListControl.ascx.cs :

public string GetDdlVendorListSelectedItemText
{
   get { return this.ddlVendorList.text; }
}

public string GetDdlVendorBUListSelectedItemText
{
   get { return this.ddlVendorBUList.text; }
}

Then from your page CreateNewRecord, you can access those properties. You just need to add an id to your control :

<%@ Register TagPrefix="uc" TagName="VendorListControl" Src="Controls/VendorListControl.ascx" %>
// Some where in the form
<tr>
    <td class="right" width="20%">Vendor Name:</td>
       <td>
          <uc:VendorListControl id="vendorListControl" runat="server" />
       </td>
</tr>

And you can access your properties like this in CreateNewRecord.aspx.cs :

ExecuteInsert(this.vendorListControl.GetDdlVendorListSelectedItemText,
  this.vendorListControl.GetDdlVendorBUListSelectedItemText, 
  MDFAmount.Text, 
  StartDate.Text,
  EndDate.Text,
  VendorQuarter.Text,
  MDFName.Text,
  MDFSummary.Text,
  Status.SelectedItem.Text,
  CreatedBy.Value
  );
查看更多
登录 后发表回答