468,115 Members | 2,106 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,115 developers. It's quick & easy.

Adding dropdownlist to a datalist

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

Nov 18 '05 #1
2 2791
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
"Shiju Poyilil" <Sh**********@discussions.microsoft.com> wrote in message
news:3C**********************************@microsof t.com...
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

Nov 18 '05 #2
Thanks Karl, your advise worked for me.

"Karl" wrote:
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
"Shiju Poyilil" <Sh**********@discussions.microsoft.com> wrote in message
news:3C**********************************@microsof t.com...
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


Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Simon | last post: by
2 posts views Thread by Przemo | last post: by
5 posts views Thread by SJ | last post: by
reply views Thread by Robert Bravery | last post: by
1 post views Thread by rn5a | last post: by
1 post views Thread by mitchman10 | last post: by
4 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.