Event handling of dynamic controls

S

Steve Caliendo

Hi,

I'm creating 5 ImageButton controls in the panel control, and I have a
unique ID specified for each one.

When I click on any one of them, the Page_Load executes (Of course), but how
do I know which ImageButton caused the post back ? Does it get handled in
the Page_Load or do I need to create a new event handler ?

Thanks,

Steve
 
K

Kevin Spencer

If I understand you correctly, you would want to create an Event Handler. I
am just not sure what you meant by a "new" Event Handler.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
S

Steve Caliendo

Ok, I tried adding this event handler and it doesn't fire when I click one
of the image buttons.

Private Sub ImageButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)

stop

end sub

Do you know what I'm doing wrong ?

Steve
 
A

ashish

ok this works for me

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim imgbtn As New ImageButton
imgbtn.ID = "myimagebtn"
imgbtn.ForeColor = System.Drawing.Color.Azure
imgbtn.Visible = True
form1.Controls.Add(imgbtn)

AddHandler CType(Page.FindControl("myimagebtn"),
ImageButton).Click, AddressOf ImageButtonClick
End Sub

Private Sub ImageButtonClick(ByVal sender As Object, ByVal e As
ImageClickEventArgs)

End Sub


hth
-ashish
 
K

Kevin Spencer

You have to wire up the Event Handler to the object's event, using An
AddHandler statement. Example:

AddHandler Button1.Click, ImageButton_Click

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
J

Juan Romero

Hey guys,

but what about child controls? I am having the same problem. I created a web
custom control that will basically draw a table with some controls inside.
One of those controls is a command button. I can't get the Onclick event of
this command button to fire. How does it work for child controls?

Thanks!
 
K

Kevin Spencer

Hi Juan,

Well, you're talking about a somewhat different scenario here, as the
original question was about handling the events fired by dynamically-added
Controls in an ASPX Page. However, there are some similarities (a Page is a
Control, and the child Controls of a Page are just that: Child Controls). In
any case, the basic principle is the same. In the case of a Page with
dynamically-added Child Controls, you have to wire up an event handler in
the Page to handle the event fired by the Child Control. In a custom Server
Control, you must do essentially the same thing. The following .Net SDK
article should be helpful:

http://msdn.microsoft.com/library/en-us/cpguide/html/cpconbubblingcommandevent.asp?frame=true

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top