473,408 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

AddHandler to Dropdownlist in ItemDataBound Doesnt work !!

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
Nov 18 '05 #1
3 7185
Hi,

you need to wire the event handler in ItemCreated because event handlers
need to be assigned on every request. ItemDataBound runs only when control
is databound, but ItemCreated runs also on postback.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke

"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

Nov 18 '05 #2
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

Nov 18 '05 #3
Hi Clouds:

Consider setting the event handler in the ASPX markup.

For example, I can set the event handler for a DropDownList inside a
DataGrid with the following:

<asp:DataGrid id="DataGrid1" runat="server"
AutoGenerateColumns="False"
OnItemDataBound="DataGrid1_ItemDataBound">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:DropDownList id="ItemDropDown"
OnSelectedIndexChanged="DropDown_SelectedIndexChan ged"
AutoPostBack="True" Runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

I'm not sure if this meets your definition of dynamic, but if all the
lists call the same event handler method it should work fine.

HTH,

--
Scott
http://www.OdeToCode.com

On Sun, 29 Aug 2004 04:03:06 -0700, Clouds
<Cl****@discussions.microsoft.com> wrote:
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


Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Stanley J Mroczek | last post by:
How do you load a dropdownlist when edit is clicked in a datagrid ? <Columns> <asp:BoundColumn DataField="OptionDescription" ItemStyle-Wrap="True" HeaderText="Option...
2
by: Dominic | last post by:
Hi guys, I'm not sure if this question belongs to FAQ, but I couldn't find a concrete answer. I created a Datagrid control using ItemTemplate, but it's NOT a in-place editing datagrid. One of...
0
by: Jeffrey A. Voigt | last post by:
Can someone take a quick glace at my code and tell me why my AutoPostBackHandler function does not get fired off at all? What I'm trying to do is get all of the Buttons and DropDownList controls...
18
by: Julia Hu | last post by:
Hi, I have a datagrid, and in different rows I need to programmatically bind different type of controls and load data into these controls. For example,in the first row I need to bind data into a...
6
by: VB Programmer | last post by:
I have an itemtemplate in a datagrid. I'm trying to set it's value based on data. Having no trouble with the textbox. But, how do I do the same thing for a dropdownlist???? <ItemTemplate>...
0
by: Luis Esteban Valencia | last post by:
Hello. I have a datagrid with one row. I have a button that adds a new row. I am trying to implement that when the user selects one product it must change the price on the quantity column. Anyway...
1
by: Luis Esteban Valencia Muñoz | last post by:
Have a dropdownlist created in my LoadMain() which is called from the Page_load: ************************PAGE LOAD********************8Private Sub Page_Load(ByVal sender As System.Object, ByVal e...
2
by: Joe | last post by:
I have a datgrid with a button column that contains link buttons. I have added code to the ItemDataBound event of the datagrid that adds a handler to each link button. Here is the code: ...
1
by: kageyone | last post by:
I have a gridview in .net 2.0 with a dropdownlist of two items - that being "active" and "inactive". <EditItemTemplate> <asp:DropDownList ID="ddlStatus" runat="server"...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.