[RESOLVED] [02/03] copy from one dropdownlist to another
I want to copy data from one dropdown list data to another one,Is it possible to do without loop?
This is how i am doing currently
Code:
ddlSerchIn.Items.Insert(0, "Data1")
ddlSerchIn.Items(0).Value = "Data1"
ddlSerchIn.Items.Insert(1, "Data2")
ddlSerchIn.Items(1).Value = "Data2"
Dim lstItems As ListItem
For Each lstItems In ddlSerchIn.Items
ddlAdvSearch1.Items.Add(lstItems)
Next
Re: [02/03] copy from one dropdownlist to another
Check .Items.CopyTo function of DDL class. It returns an array of ListItem.
VB Code:
Dim lstItems As New ListItem[ddlSerchIn.Items.Count]
ddlSerchIn.Items.CopyTo(lstItems, 0)
ddlAdvSearch1.DataSource = lstItems
ddlAdvSearch1.DataBind()
Just wrote from top of my head.
Edit: changing language from C# to VB
Re: [02/03] copy from one dropdownlist to another
One more point to be made:
If the first DDL is bound to a datasource, you can just retrieve its datasource, pass it to the other one and call DataBind() of second DDL.
Re: [02/03] copy from one dropdownlist to another
Thanks for the code snnipet,here is vb ver of same...
Code:
Dim lstItems As ListItem() = New ListItem(ddlSerchIn.Items.Count -1){}
ddlSerchIn.Items.CopyTo(lstItems, 0)
ddlAdvSearch1.AddRange(lstItems )