Move items from one DropDownList to other one

In this blog we will know how to move items from one DropDownList to other one.

 

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <asp:DropDownList ID="DropDownList1" runat="server"> </asp:DropDownList>

    <asp:Button ID="Button1" runat="server" Text="&lt;|" onclick="Button1_Click" />

   <asp:Button ID="Button2" runat="server" Text="|&gt;" onclick="Button2_Click" />

   <asp:Button ID="Button3" runat="server" Text="&lt;&lt;|" onclick="Button3_Click" />

   <asp:Button ID="Button4" runat="server" Text="|&gt;&gt;" onclick="Button4_Click" />

   <asp:DropDownList ID="DropDownList2" runat="server"> </asp:DropDownList>

    </div>

    </form>

</body>

</html>

 

 

using System;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            DropDownList1.Items.Add("Raj");

            DropDownList1.Items.Add("Ravi");

            DropDownList1.Items.Add("Rahul");

            DropDownList1.Items.Add("Raju");

            DropDownList1.Items.Add("Rohit");

            DropDownList1.Items.Add("Rajesh");

        }

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        DropDownList1.Items.Add(DropDownList2.SelectedItem.Text);

        int i = 0;

        i = DropDownList2.SelectedIndex;

        DropDownList2.Items.RemoveAt(i);

    }

    protected void Button2_Click(object sender, EventArgs e)

    {

        DropDownList2.Items.Add(DropDownList1.SelectedItem.Text);

        int i = 0;

        i = DropDownList1.SelectedIndex;

        DropDownList1.Items.RemoveAt(i);

    }

    protected void Button3_Click(object sender, EventArgs e)

    {

        int j = 0;

        for (j = 0; j <= DropDownList2.Items.Count - 1; j++)

        {

            DropDownList1.Items.Add(DropDownList2.Items[j]);

        }

        DropDownList2.Items.Clear();

    }

    protected void Button4_Click(object sender, EventArgs e)

    {

        int j = 0;

        for (j = 0; j <= DropDownList1.Items.Count - 1; j++)

        {

            DropDownList2.Items.Add(DropDownList1.Items[j]);

        }

        DropDownList1.Items.Clear();

    }

}