determining what control caused the click event

M

matthias s

Hi there,

probably a stupid question but i can't figure it out:

I've got a UserControl with a PlaceHolder on it. In my Page_Load I
dynamically add a couple of ImageButtons to that PlaceHolder and add an
EventHandler for the click-event of each ImageButton (say "Button_Click").
The same handler is used for all ImageButtons. When I now click one of the
buttons on my page, the EventHandler gets called ok, but I can't determine
exactly which ImageButton the user clicked.

here is the code adding the image buttons:

for (int i = 0; i < 5; i++)
{
ImageButton btn = new ImageButton();
btn.ImageUrl = "some valid url... omitted";
btn.ToolTip = "test";
btn.AlternateText = "test";

btn.Click += new ImageClickEventHandler(Button_Click);
}

I guess I'm looking for something like a Tag Property of an ImageButton
allowing me to store something like an ID or so.

That must be a very common problem. I guess they teach something like this
within the first 5 hours of "Learn to become a aspx developer." ;)


Any help is greatly apreciated. Thanks in advance,

Matthias
 
D

David R. Longnecker

Matthias-

ImageButtons (and almost all objects) have an Id property that is read/write.


HTH.

-dl
 
M

matthias s

thanks david.

if i set the ID property to something useful, the value is not persisted on
the next roundtrip, i always get a _ctl0 value in my handler, which looks
like this:

private void Button_Click(object sender, ImageClickEventArgs e)
{
ImageButton langButton = (ImageButton) sender;
Trace.WriteLine("langButton.ID");
}

can you still help?

thank you in advance! matthias
 
M

matthias s

sorry david, please ignore my previous msg. was a coding error on my side (i
set the id value to an empty string, thus an id was generated for me). i
gotta get some more sleep, i guess ;)

kind regards, matthias
 
D

David R. Longnecker

Using the following code (based on yours), I simply added the ImageButtons
to the current form. The click events fire accordingly and using the btn.Id,
I can grab back Id#1, Id#2, or whatever. I'm basing the Id on the "i" for
loop variable, but it could be something out of a database.


protected void Page_Load(object sender, EventArgs e)
{
this.Trace.IsEnabled = true;
for (int i = 0; i < 5; i++)
{
ImageButton btn = new ImageButton();
btn.ImageUrl = "some valid url... omitted";
btn.ToolTip = "test";
btn.ID = "Id#" + i;
btn.AlternateText = "test";
btn.Click += new ImageClickEventHandler(Button_Click);
form1.Controls.Add(btn);
}

}

protected void Button_Click(object sender, ImageClickEventArgs e)
{
ImageButton btn = sender as ImageButton;
Trace.Write("Button: " + btn.ID);

}

When the third button (zero-based) is clicked, you get:

aspx.page Begin Raise PostBackEvent 0.0001226 0.000010
Button: Id#2 0.00016528 0.000043
aspx.page End Raise PostBackEvent 0.00017804 0.000013

Dynamic controls are rebuilt each time, but as long as you're assigning the
same Id each time, it should persist. If you wanted to capture what was
clicked, you could toss that into ViewState or Session and handle that on
the next Page_Load.

HTH.

-dl
 
D

David R. Longnecker

Ahh, okay. Then ignore my trial and error and code. Glad you got it working!

-dl
 

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,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top