I am trying to bind a DropDownList control to various data store. In my case the data store are arrays file values and Item Texts.
This is a short example of my 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>
The code behind (C#) will dynamically generate a DropDownList control from two arrays.
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 ";
So, till here everything is fine except that I need a categoryId for every item in DropDownSubCategories DropDownList control as well, otherwise I wouldn't be able to retrieve any product from an external database, since I need a name + categoryid in order to display a product description.
My question is, Is there any way to add two values ( one as a Value and the other as Item.Text ) to an array, so I can bind both to the DropDownList control ?
Thank you, any help or even an alternative suggestion will be appreciate it.