Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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

Report this thread to moderator Post Follow-up to this message
Old Post
Clouds
08-29-04 01:57 PM


Re: AddHandler to Dropdownlist in ItemDataBound Doesnt work !!
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" <Clouds@discussions.microsoft.com> wrote in message
news:739D2432-8C87-4FF6-93E6-69C81BB643FB@microsoft.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



Report this thread to moderator Post Follow-up to this message
Old Post
Teemu Keiski
08-29-04 08:58 PM


Re: AddHandler to Dropdownlist in ItemDataBound Doesnt work !!
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" <Clouds@discussions.microsoft.com> wrote in message
news:739D2432-8C87-4FF6-93E6-69C81BB643FB@microsoft.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



Report this thread to moderator Post Follow-up to this message
Old Post
Karl
08-29-04 08:58 PM


Re: AddHandler to Dropdownlist in ItemDataBound Doesnt work !!
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_SelectedIndexChanged"
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
<Clouds@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 dropdow
n
>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


Report this thread to moderator Post Follow-up to this message
Old Post
Scott Allen
08-30-04 08:58 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

ASP .NET archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:56 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.