DropDownList SelectedIndex Property

A

Ashish Sharma

I have a DropDownList inside a DataList on a page. I want to set the
selectedindex property of the DropDownList to some value.

I bind the DropDownList on the ItemCreated Event of the DataList.

Private Sub DataList1_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataListItemEventArgs) Handles
DataList1.ItemCreated
Dim ddl As DropDownList
ddl = CType(e.Item.FindControl("DropDownList1"), DropDownList)
ddl.DataSource = GetDropDownListData
ddl.DataBind()
ddl.SelectedIndex = 2
end sub

but this does not sets the selected index and the selected index comes
out to be default ( i.e 0)

Where should i set this property so that it displays on the rendered code

Any help would be appreciated

TIA
-ashish
 
T

Teemu Keiski

Hi,

run this code in ItemDataBound instead of ItemCreated. In ItemCreated,
DataListItem itself is not yet added to the Controls collection, which means
its child controls are neither. This means that changes made to the control
state, like databinding, are not stored to ViewState. Though you are doing
this in ItemCreated, when it is run on every request and should otherwise
replace the databinding behaviour made in ItemDataBound, but rebinding the
grid will put the SelectedIndex back to 0. Besides ItemCreated can run twice
on same request (when DataList is restored from ViewState as well as
rebound), so databinding multiple times is actually also a waste of
resources.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke


I have a DropDownList inside a DataList on a page. I want to set the
selectedindex property of the DropDownList to some value.

I bind the DropDownList on the ItemCreated Event of the DataList.

Private Sub DataList1_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataListItemEventArgs) Handles
DataList1.ItemCreated
Dim ddl As DropDownList
ddl = CType(e.Item.FindControl("DropDownList1"), DropDownList)
ddl.DataSource = GetDropDownListData
ddl.DataBind()
ddl.SelectedIndex = 2
end sub

but this does not sets the selected index and the selected index comes
out to be default ( i.e 0)

Where should i set this property so that it displays on the rendered code

Any help would be appreciated

TIA
-ashish
 
A

Ashish Sharma

Teemu said:
Hi,

run this code in ItemDataBound instead of ItemCreated. In ItemCreated,
DataListItem itself is not yet added to the Controls collection, which means
its child controls are neither. This means that changes made to the control
state, like databinding, are not stored to ViewState. Though you are doing
this in ItemCreated, when it is run on every request and should otherwise
replace the databinding behaviour made in ItemDataBound, but rebinding the
grid will put the SelectedIndex back to 0. Besides ItemCreated can run twice
on same request (when DataList is restored from ViewState as well as
rebound), so databinding multiple times is actually also a waste of
resources.
hey Thanks for explanation Teemu,
it works now !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top