gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 05:30:10 24 4
gpt4 key购买 nike

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

最佳答案

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

关于ASP.NET C# - 使用用户控件的下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12731786/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com