DropDown List Selection
I have three DropDown List Controls.each dropdown contains the static values 1,2,3,4开发者_开发知识库 what i need to do is when a item is selected on the first dropdown(on SelectedIndexChanged Event) same items should be selected in other two dropdowns
Here's what you do.
In the ASPX code:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_SelectedIndexChanged">
<asp:ListItem Text="1" Value="1" />
<asp:ListItem Text="2" Value="2" />
<asp:ListItem Text="3" Value="3" />
<asp:ListItem Text="4" Value="4" />
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_SelectedIndexChanged">
<asp:ListItem Text="1" Value="1" />
<asp:ListItem Text="2" Value="2" />
<asp:ListItem Text="3" Value="3" />
<asp:ListItem Text="4" Value="4" />
</asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_SelectedIndexChanged">
<asp:ListItem Text="1" Value="1" />
<asp:ListItem Text="2" Value="2" />
<asp:ListItem Text="3" Value="3" />
<asp:ListItem Text="4" Value="4" />
</asp:DropDownList>
In the codebehind:
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
string value = ddl.SelectedValue;
SetValue(DropDownList1, value);
SetValue(DropDownList2, value);
SetValue(DropDownList3, value);
}
protected void SetValue(DropDownList ddl, string value)
{
ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(value));
}
Just typing ddl1.SelectedValue = ddl2.SelectedValue
won't work, because DropDownList.SelectedValue
is read-only.
Note that I didn't just set the SelectedIndex
of all the DDLs to that of the sender. You can use that in your example scenario, but if one of your DDLs ever has its ListItems in a different order from the others, the code will break. In my opinion, this makes it dangerous practice, but YMMV.
Also, if you decide to generalize the SetValue method (I often add it as an extension method to the DropDownList
control across the entire project), you should handle cases where the target value isn't found in the DDL, presumably by throwing an exception. You can also make a SetText version using FindByText
.
Set the selectedvalue property on 2nd and 3rd drop down to the selected value on the first. Set the DropDown to autopostback="true" so that it posts back to the server, and you can set it appropriately.
OR, use client-side JavaScript to change the selectedindex property on the other select elements when the change client event fires for the first.
All you need to do is set the SelectedIndex
of your other drop down lists like this:
protected void DropDownList1SelectedIndexChanged(Object sender, EventArgs e)
{
dropDownList2.SelectedIndex = dropDownList1.SelectedIndex;
dropDownList3.SelectedIndex = dropDownList1.SelectedIndex;
}
Try the following:
Markup:
<asp:DropDownList ID="dd1" OnSelectedIndexChanged="dd1_SelectedIndexChanged" AutoPostBack="true" runat="server" />
<asp:DropDownList ID="dd2" runat="server" />
<asp:DropDownList ID="dd3" runat="server" />
Code-behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
BindData();
}
protected void dd1_SelectedIndexChanged(object sender, EventArgs e)
{
string selected = dd1.SelectedValue;
dd1.SelectedValue = dd2.SelectedValue = dd3.SelectedValue = selected;
BindData();
}
private void BindData()
{
int[] values = { 1, 2, 3, 4 };
dd1.DataSource = dd2.DataSource = dd3.DataSource = values;
dd1.DataBind();
dd2.DataBind();
dd3.DataBind();
}
精彩评论