ASP.NET C# - 使用用户控件下拉列表

时间:2012-10-04 16:35:31

标签: asp.net webforms

我是ASP.NET新手

这个论坛中的某个人帮助我如何让用户控制下拉列表工作,并且它正在运行。

在我的用户控制文件VendorListControl.ascx中,我有以下代码。请假设VendorListControl.ascx.cs正常工作,即当我选择VendorName时,它将触发“ddlVendor_SelectedIndexChanged”以刷新“ddlVendorBUList”下拉列表。

<%@ 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" /> 

我的VendorListControl.ascx.cs代码:

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";
            }
        }

    }
}

接下来,在我的CreateNewRecord.aspx页面中,我将此代码包含在用户控件的下拉列表中。我可以看到下拉列表正常工作。

<%@ 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>

问题与这两个下拉列表的ID有关。当我尝试执行插入记录时,似乎没有检测到“ddlVendorList”和“ddlVendorBUList”的ID来自用户控制ascx页面。错误“名称'ddlVendorList'在当前上下文中不存在”

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>");

我知道我是ASP.NET的新手,所以请帮忙。

2 个答案:

答案 0 :(得分:2)

您可以在VendorListControl中放置两个属性,以获取ddlVendorList selectedItem文本和ddlVendorBUList selectedItem文本。

在VendorListControl.ascx.cs中:

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

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

然后,从您的页面CreateNewRecord,您可以访问这些属性。您只需要为控件添加一个ID:

<%@ 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>

您可以在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
  );

答案 1 :(得分:1)

您定义在您的UserControl中返回public property的{​​{1}}。

用户控制(Ascx)

SelectedItem.Text

Page(Aspx)

您可以使用您的公共财产:YourValue。

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

}

注意:如果您希望设置值,则在属性上定义setter

更新后添加这些属性

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