You must Sign In to post a response.
  • Category: ASP.NET

    Swaping the value bettween two dropdownlist

    i have two dropdownlist and one button when i click the button then the value will be swaped between these two dropdownlist
  • #768631
    Hi,

    I feel this is purely logic and code basis. I mean no special effort required to fix this implementation.
    Suppose you have two dropdownlist and you have named it D1 and D2. During the button click event we need to interchange the entire values of dropdownlist from one to another.


    D1.Items.Add(new ListItem("Sub1", "1"));
    D1.Items.Add(new ListItem("Sub2", "2"));
    D1.Items.Add(new ListItem("Sub3", "3")); // Now you have got the list of values in first Dropdownlist

    D2.Items.AddRange(drpTypes.Items); // Here during button click event we are moving D1 values to D2 values

    and you can do the viceversa for D2 to D1

    D1.SelectedValue = "2"; // If you particularly want to choose the current position after the swap
    D2.SelectedValue = "3";

    Thanks,
    Mani

  • #768666
    You can use following code snippet for Swaping the value between two dropdownlist and one button
    <form id="form1" runat="server">
    <div>
    <asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem Text="One" Value="0" />
    <asp:ListItem Text="Two" Value="1" />
    </asp:DropDownList>
    <asp:DropDownList ID="DropDownList2" runat="server">
    <asp:ListItem Text="One" Value="0" />
    <asp:ListItem Text="Two" Value="1" />
    </asp:DropDownList>
    <asp:Button ID="btnChangeSelection" runat="server" OnClick="ChangeSelection" Text="Change DropDownList Values" />
    </div>
    </form>
    C#:
    protected void ChangeSelection(object sender, EventArgs e)
    {
    int i = this.DropDownList1.SelectedIndex;
    this.DropDownList1.SelectedIndex = this.DropDownList2.SelectedIndex;
    this.DropDownList2.SelectedIndex = i;
    }


Sign In to post your comments