Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > AddHandler to Dropdownlist in ItemDataBound Doesnt work !!

Reply
Thread Tools

AddHandler to Dropdownlist in ItemDataBound Doesnt work !!

 
 
=?Utf-8?B?Q2xvdWRz?=
Guest
Posts: n/a
 
      08-29-2004
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
 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      08-29-2004
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" <> wrote in message
news:739D2432-8C87-4FF6-93E6-...
> 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



 
Reply With Quote
 
 
 
 
Karl
Guest
Posts: n/a
 
      08-29-2004
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" <> wrote in message
news:739D2432-8C87-4FF6-93E6-...
> 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



 
Reply With Quote
 
Scott Allen
Guest
Posts: n/a
 
      08-30-2004
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:

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

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
<> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sort list doesnt work, key=str still doesnt work notnorwegian@yahoo.se Python 3 05-27-2008 04:32 AM
AddHandler in C# doesn't work Bjorn Sagbakken ASP .Net 5 10-29-2007 02:49 PM
PC doesnt boot first time and doesnt shutdown dann Computer Support 6 08-21-2006 07:31 AM
AddHandler doesn't seem to work. SimonM ASP .Net Web Controls 3 06-04-2005 07:35 AM
AddHandler doesn't work in Page_PreRender() news.microsoft.com ASP .Net Web Controls 0 10-21-2004 04:05 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57