ItemDataBound isn't invoked on postback because the control isn't databound
back to the source, the viewstate is used. If you put a breakpoint/trace in
ItemDataBound you'll see it isn't called. handlers added dynamically don't
preserve their state on postback, so you need to add them somewhere around
the page_load event (not exactly sure what's the latest you can get away
with).
You can either put the code in the ItemCreated or in Page_Load if
Page.IsPostBack is true.
Private Sub List_ItemDataBound(ByVal sender As Object, ByVal e As
DataListItemEventArgs) Handles list.ItemDataBound
Dim ddl As DropDownList = CType(e.Item.FindControl("ddl"),
DropDownList)
AddHandler ddl.SelectedIndexChanged, AddressOf ddl_Changed
End Sub
Sub Page_Load...
IF Page.IsPostBack = true THEN
For Each item As DataListItem In list.Items
Dim ddl As DropDownList = CType(item.FindControl("ddl"), DropDownList)
AddHandler ddl.SelectedIndexChanged, AddressOf ddl_Changed
Next
end if
end sub
I realize that whether to bind or not is based on your datasource, but
again, you don't have access to the data source on postback. What I would
recommend is that you use a hidden form field <input type="hidden"
runat="server" id="doPostback" /> and on the ItemDataBound, you store true
or false in there based on whatever rule you have. Then on postback, using
either method above, get that field, check if the value is true or false, if
true, hook up the handler.
Karl
"Clouds" <Cl****@discussions.microsoft.com> wrote in message
news:73**********************************@microsof t.com...
Hi !
How do I add the dynamic event handler for a dropdownlist present in the
itemtemplate of a datalist !!
I am doing it in the itemdatabound event of the datalist but it doesnt
work... I am also setting the autopostback property to true for the
dropdown list and it works but the handler doesnt get invoked at runtime...
I have to do it in itemdatabound becaz whether to add the handler or not
is driven based on the information which i have in datasource...
Whats happening to the handler ??
Regards
--
Clouds