Home All Groups Group Topic Archive Search About

determining what control caused the click event

Author
19 Sep 2007 7:06 PM
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

Author
20 Sep 2007 12:18 AM
David R. Longnecker
Matthias-

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


HTH.

-dl

--
David R. Longnecker
http://blog.tiredstudent.com

Show quote
> 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
>
Author
20 Sep 2007 11:16 AM
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

Show quote
"David R. Longnecker" <dlongnecker@community.nospam> schrieb im Newsbeitrag
news:2ab04a2812daa8c9c908fa9f0237@msnews.microsoft.com...
> Matthias-
>
> ImageButtons (and almost all objects) have an Id property that is
> read/write.
>
> HTH.
>
> -dl
>
> --
> David R. Longnecker
> http://blog.tiredstudent.com
>
>> 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
>>
>
>
Author
20 Sep 2007 12:11 PM
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

--
David R. Longnecker
http://blog.tiredstudent.com

Show quote
> 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
>
> "David R. Longnecker" <dlongnecker@community.nospam> schrieb im
> Newsbeitrag news:2ab04a2812daa8c9c908fa9f0237@msnews.microsoft.com...
>
>> Matthias-
>>
>> ImageButtons (and almost all objects) have an Id property that is
>> read/write.
>>
>> HTH.
>>
>> -dl
>>
>> --
>> David R. Longnecker
>> http://blog.tiredstudent.com
>>> 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
>>>
Author
20 Sep 2007 11:55 AM
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


Show quote
"David R. Longnecker" <dlongnecker@community.nospam> schrieb im Newsbeitrag
news:2ab04a2812daa8c9c908fa9f0237@msnews.microsoft.com...
> Matthias-
>
> ImageButtons (and almost all objects) have an Id property that is
> read/write.
>
> HTH.
>
> -dl
>
> --
> David R. Longnecker
> http://blog.tiredstudent.com
>
>> 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
>>
>
>
Author
20 Sep 2007 12:11 PM
David R. Longnecker
Ahh, okay.  Then ignore my trial and error and code.  Glad you got it working!

-dl

--
David R. Longnecker
http://blog.tiredstudent.com

Show quote
> 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
>
> "David R. Longnecker" <dlongnecker@community.nospam> schrieb im
> Newsbeitrag news:2ab04a2812daa8c9c908fa9f0237@msnews.microsoft.com...
>
>> Matthias-
>>
>> ImageButtons (and almost all objects) have an Id property that is
>> read/write.
>>
>> HTH.
>>
>> -dl
>>
>> --
>> David R. Longnecker
>> http://blog.tiredstudent.com
>>> 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
>>>

AddThis Social Bookmark Button