Home All Groups Group Topic Archive Search About

AddHandler doesn't seem to work.

Author
2 Jun 2005 3:22 PM
SimonM
I am creating an ImageButton at run time and adding it to a place holder
control's collection. I am using the AddHandler to associate a procedure with
the Command event but when I click on the ImageButton at runtime, it doesn't
fire the procedure...

Here is my code:

' Add Image Button
ibnConfirm = New ImageButton
ibnConfirm.ImageUrl = "Confirm16.ico"
ibnConfirm.CommandName = "Confirm"
ibnConfirm.CommandArgument = e.Item.DataItem("pk_Quotation_in")
ibnConfirm.ToolTip = "Confirm Quotation"

AddHandler ibnConfirm.Command, AddressOf ibnConfirm_Command

' Add to place holder collection
plhConfirm.Controls.Add(ibnConfirm)

Author
2 Jun 2005 4:32 PM
Teemu Keiski
And you add the ImageButton similarly on postback as well?

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
Author
3 Jun 2005 7:22 AM
SimonM
My control is being added in the ItemDataBound event of a Repeater control
(It may be added once per row of data).

So presumably, that is ok?
Author
4 Jun 2005 7:35 AM
Teemu Keiski
Hi,

if adding in ItemdataBound indeed means instantiating there and adding to
the Row's Controls, that's the reason why it wouldn't work because it isn't
there by the time when postback happens (until rebinding the grid).
ImageButton here is a dynamical control and it needs to be added to the
Controls collection on every request( for the time it is needed) for
everything to work smoothly. Events won't be raised on postback if the
control is not there.

So that the control would exist there as is needed for postback data
handling, it would need to be instantiated also in ItemCreated. You probably
detect from data that should the control be created which makes it harder to
manage, because you really *need* also to instantiate the control in
ItemCreated but the info comes from ItemdataBound

So you could check in ItemDataBound  if data indicates a control needs to be
created  (on databinding ItemCreated is first called and then ItemDataBound,
on postback when not binding only ItemCreated is called --> causes the basic
problem with dynamical controls) . You'd need to store on row-basis (say to
ViewState) if the control needs to recreated on following requests and then
based on this info cause also instation in ItemCreated.

So idea :

1. In ItemDataBound check the data, should the control be created and if it
should create it there. Set a row-level flag (to viewstate) that the control
is created

2. In ItemCreated check the existence of this flag and based on it, recreate
the control as needed

I also have a few pointers for you, the same thing is discussed here
http://forums.asp.net/674362/ShowPost.aspx
http://forums.asp.net/745711/ShowPost.aspx

And there's also a free control that might be able to help you.
http://forums.asp.net/745711/ShowPost.aspx


--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU