472,953 Members | 1,880 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,953 software developers and data experts.

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 2994
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Umut K. | last post by:
Hi all, I've a DataList control and it's DataBound by a SqlDataReader... The Reader returns say 3 records and as expected the datalist shows 3 rows. What i want to do is to add another extra...
4
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
0
by: Simon | last post by:
All I have a .Net 2003 webform, that contains a DropDownList databound to one table, whilst SelectedValue is databound to another table I discovered that the first time I'd load the page, the...
2
by: Przemo | last post by:
Hi, I have a DropDownList, which is based on a DataTable generated from SQLServer. It consists of two fields: ID and Name. ID starts from "1" I Would like to add a special row to my...
0
by: bryanp10 | last post by:
I have a DataList on my page which contains multiple DropDownLists. My page defaults to showing six rows in the DataList. I want the ability to dynamically add rows if needed. Right now I'm just...
5
by: SJ | last post by:
I have a dropdownlist("myList") in my DataList control. When a user selects one of the items in the dropdownlist, I need to access the value of the DataBound Label ("myLabel") in the Datalist and...
0
by: Robert Bravery | last post by:
HI All, I'm new to C# and .NET I have a datagridview that shwos the results of the following command select ordinal_position 'Seq',cast(column_name as varchar(40))...
1
by: rn5a | last post by:
An ASP.NET Form has different server controls like Panels, Labels, TextBoxes, HiddenFields etc. The Form has a DataList as well. This is how the DataList looks: <form runat="server">...
1
by: mitchman10 | last post by:
My Time table has TimeID,Employee,PayPeriod,ChargeCodeID,Hours My Chargecode table has ChargecodeID,c_Text I need an Editable datagrid that will show the TimeID,Employee,PayPeriod,C_Text in a...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.