Adding dropdownlist to a datalist

G

Guest

Hi !
I have a requirement wherein i am binding a datalist which contains a label
(Caption for the field) and some literal hidden fields and a dropdown list.
When I am binding to the datalist.. only the labels (caption) and the
invisible literal controls are binded..
Now based on the invisible literal control values I get the information from
DB to bind it with each dropdown list (I am doing this in the item databound
event)
Also based on one of the literal control value i have to add the event
handler to the drop down list... e.g. Refer the code

' Code to bind the datalist

dim arlList as ArrayList
arlList = objService.GetServicesDetailsRequired(..)
dtlList.datasource = arlList
dtlList.databind()

' Now in the item databound event of the datalist --

' Get the details from DB for the given values

dim arlDetails as Arraylist =
objService.GetServiceList(Ctype(e.item.findcontrol("litCodeType"),
Literal).Text, Ctype(e.item.findcontrol("litServiceCode"), Literal).Text)

dim ddl as dropdownlist = Ctype(e.item.findcontrol("ddlDetails"),
dropdownlist)
ddl.datasource = arlDetails
ddl.databind()

' till this point the code is working..
' now based on the current items one of the listeral controls value.. i have
to add
' the handler like e.g. if the current dropdown list depends on one of the
previous
' dropdownlist

dim currentDetailCode as string = Ctype(e.item.findControl("litCode"),
Literal).text
for each dtlItem in the datalist
if Ctype(dtlItem.findControl("litPrevDetailCode"), Literal).text =
currentDetailCode then
ddl.AutoPostBack = true
AddHandler ddl.SelectedIndexChanged, AddressOf <SomeMethodName>
end if
....

' Here my item databound code for the datalist ends

but my event handler is not invoked at runtime
what is the problem ? I am not able to understand why is it not firing the
event..

For some simple purpose one of my friend has done this in item created event
... but for him .. he knows that there will be always an event handler for the
dropdown list.. but for me its not the same situation .. i have to depend on
some binded values.. which i can access only in item databound event...


your expert advise will be highly appreciated.

Rgds
Shiju
 
K

Karl

Shiju,
I just responded to another thread that was very similar. I'm not sure that
I follow your code, but here's what I said, hope this is relevant:

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
 
G

Guest

Thanks Karl, your advise worked for me.

Karl said:
Shiju,
I just responded to another thread that was very similar. I'm not sure that
I follow your code, but here's what I said, hope this is relevant:

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
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top