Tuesday 22 April 2014

Transfer the content from DropdownList1 to DropdownList2 and vice versa.



Create a web application with two DropdownList1 & DropdownList2 and design application to transfer the  content from  DropdownList1 to DropdownList2  and vice versa.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ADO.net_Tutorial
{
    public partial class WebForm6 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ListItem selecteditem = new ListItem("Select", "7");

                ListItem item1 = new ListItem("One", "1");
                ListItem item2 = new ListItem("Two", "2");
                ListItem item3 = new ListItem("Three", "3");

                ListItem item4 = new ListItem("Four", "4");
                ListItem item5 = new ListItem("Five", "5");
                ListItem item6 = new ListItem("Six", "6");

                selecteditem.Selected = true;
               DropDownList1.Items.Add(selecteditem);
               DropDownList2.Items.Add(selecteditem);

               DropDownList1.Items.Add(item1);
               DropDownList1.Items.Add(item2);
                DropDownList1.Items.Add(item3);

             DropDownList2.Items.Add(item4);
             DropDownList2.Items.Add(item5);
             DropDownList2.Items.Add(item6);

            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            ListItem[] List1Array = new ListItem[DropDownList1.Items.Count];
           DropDownList1.Items.CopyTo(List1Array, 0);

            ListItem[] List2Array = new ListItem[DropDownList2.Items.Count];
           DropDownList2.Items.CopyTo(List2Array, 0);

           DropDownList1.Items.Clear();
            DropDownList2.Items.Clear();
          
            foreach (ListItem myItem in List1Array)
            {                
                DropDownList2.Items.Add(myItem.Text);
            }

            foreach (ListItem myItem in List2Array)
            {
           DropDownList1.Items.Add(myItem.Text);
            }
        }
    }
}

Output:-



If anyone have any doubt about the program , please comment and you can also mail me on "ibsarmschauhan13@gmail.com" and "makansingh42@yahoo.com".

Thank you so much :)

No comments:

Post a Comment