Transfer DropDownList All Items to another DropDownList Control in ASP.Net

Here, In asp.net tutorials of dropdownlist control we will learn how to move or transfer all the Items of DropDownList control to another DropDownList control programmatically in asp.net with C#.

Here, we need to use for loop for all all items to another DropDownList control on Button Click Events.

We use DropDownList1.Items.Add(“ItemName”) for add new item to dropdownlist control in asp.net.

Exmaple:

First Design the asp.net web form like below:

<table>
<tr>
<td style=”width: 100px; text-align: right; height: 24px;”>
Value :</td>
<td style=”width: 100px; height: 24px;”>
<asp:DropDownList ID=”DropDownList1″ runat=”server” Width=”128px”>
<asp:ListItem>ABC</asp:ListItem>
<asp:ListItem>DEF</asp:ListItem>
<asp:ListItem>Meera</asp:ListItem>
<asp:ListItem>Academy</asp:ListItem>
</asp:DropDownList></td>
<td style=”width: 100px; height: 24px;”>
</td>
</tr>
<tr>
<td colspan=”3″ style=”text-align: right”>
&nbsp; &nbsp;
<asp:Button ID=”Button1″ runat=”server” OnClick=”Button1_Click” Text=”Transfer to single” /><asp:Button
ID=”Button2″ runat=”server” OnClick=”Button2_Click” Text=”Transfer to Both” /></td>
</tr>
<tr>
<td style=”width: 100px; text-align: right; height: 24px;”>
</td>
<td style=”width: 100px; text-align: left; height: 24px;”>
<asp:DropDownList ID=”DropDownList2″ runat=”server” Width=”128px”>
</asp:DropDownList></td>
<td style=”width: 100px; text-align: left; height: 24px;”>
<asp:DropDownList ID=”DropDownList3″ runat=”server” Width=”128px”>
</asp:DropDownList></td>
</tr>
</table>

The Design output of asp.net dropdownlist control example is:

Transfer Items from one DropDownList control to other DropDownList Control in asp.net
Transfer Items from one DropDownList control to other DropDownList Control in asp.net

Now, Here we have two button control, when we click Transfer to Single that means all items transfer to only one DropDownList control and click Transfer to Both the all Items transfer to both DropDownList Contol in asp.net C#.

write below C# code at Transfer to Single Button click event:

protected void Button1_Click(object sender, EventArgs e)
{
DropDownList2.Items.Clear();
DropDownList3.Items.Clear();
for (int i = 0; i < DropDownList1.Items.Count; i++)
{
DropDownList2.Items.Add(DropDownList1.Items[i].Text);
}
}

The asp.net DropDownList control Transfer Item Example output is:

Transfer Items from one DropDownList control to other DropDownList Control in asp.net
Transfer Items from one DropDownList control to other DropDownList Control in asp.net

After doing this, write below code at Transfer to Both Button click event at coding page of asp.net webform.

protected void Button2_Click(object sender, EventArgs e)
{
DropDownList2.Items.Clear();
DropDownList3.Items.Clear();

for (int i = 0; i < DropDownList1.Items.Count; i++)
{
DropDownList2.Items.Add(DropDownList1.Items[i].Text);
DropDownList3.Items.Add(DropDownList1.Items[i].Text);
}
}

The asp.net DropDownList control Transfer Item Example output is:

Transfer Items from one DropDownList control to other DropDownList Control in asp.net
Transfer Items from one DropDownList control to other DropDownList Control in asp.net

I hope this asp.net DropDownList control example will help you……..

Leave a Reply

Your email address will not be published. Required fields are marked *