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
"Ashish Sharma" <as****@mailmenot.com> wrote in message
news:OF**************@TK2MSFTNGP12.phx.gbl...
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