2

我是 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 的新手,所以请帮忙。

4

2 回答 2

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
  );
于 2012-10-04T16:40:24.970 回答
1

您可以在 UserControl 中定义public property谁返回SelectedItem.Text

用户控制 (Ascx)

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

}

页面 (aspx)

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

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

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

更新后添加这些属性

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

public string YourDdlVendorBUListSelectedItemText
{
   get 
   { 
     return this.ddlVendorBUList.text; 
   }
}
于 2012-10-04T16:41:56.727 回答