Friday, January 4, 2013

Select Same Value on Multiple DropDowns in ASP.NET


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.

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 code behind add below

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));
}


No comments:
Write comments
Recommended Posts × +