【发布时间】:2023-03-11 03:16:01
【问题描述】:
我正在尝试将 DropDownList 控件绑定到各种数据存储。在我的情况下,数据存储是数组文件值和项目文本。
这是我的 HTML 的一个简短示例:
<asp:DropDownList ID="DropDownListDealCategory"
runat="server"
Height="25px"
Width="150px"
AutoPostBack="true"
OnSelectedIndexChanged="DropDownListDealCategory_SelectedIndexChanged">
<asp:ListItem Selected="True" Value="0">-- Select Category --</asp:ListItem>
<asp:ListItem Value="10">Electronics</asp:ListItem>
<asp:ListItem Value="22">Computer</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownSubCategories"
runat="server"
Visible="false"
Height="25px"
Width="170px"
AutoPostBack="true"
OnSelectedIndexChanged="DropDownSubCategories_SelectedIndexChanged">
</asp:DropDownList>
(C#) 后面的代码将从两个数组动态生成一个 DropDownList 控件。
protected void DropDownDealCategory_SelectedIndexChanged(object sender, EventArgs e)
{
string[] Electronics = new[] { " Select Subcategory ", "Cameras and Photography", "Home Audio"};
string[] Computer = new[] {" Select Subcategory " "Laptops", "Monitors"};
if (DropDownListCategory.SelectedItem.Text == "Electronics")
{
DropDownSubCategories.DataSource = Electronics;
}
else if (DropDownListDealCategory.SelectedItem.Text == "Computer")
{
DropDownSubCategories.DataSource = Computer;
}
DropDownSubCategories.DataBind();
DropDownSubCategories.Visible = DropDownListCategory.SelectedItem.Text != " Select Category ";
所以,到这里为止一切都很好,除了我还需要 DropDownSubCategories DropDownList 控件中的每个项目都需要一个 categoryId,否则我将无法从外部数据库中检索任何产品,因为我需要一个 name + categoryid 来显示产品描述。
我的问题是,有没有办法将两个值(一个作为 Value,另一个作为 Item.Text )添加到数组中,这样我就可以将两者都绑定到 DropDownList 控件?
谢谢,任何帮助甚至替代建议都将不胜感激。
【问题讨论】:
标签: c# asp.net arrays drop-down-menu