determining what control caused the click event

Discussion in 'ASP .Net Web Controls' started by matthias s, Sep 19, 2007.

  1. matthias s

    matthias s Guest

    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
    matthias s, Sep 19, 2007
    #1
    1. Advertising

  2. 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
    >
    David R. Longnecker, Sep 20, 2007
    #2
    1. Advertising

  3. matthias s

    matthias s Guest

    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" <> schrieb im Newsbeitrag
    news:...
    > 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
    >>

    >
    >
    matthias s, Sep 20, 2007
    #3
  4. matthias s

    matthias s Guest

    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" <> schrieb im Newsbeitrag
    news:...
    > 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
    >>

    >
    >
    matthias s, Sep 20, 2007
    #4
  5. 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

    > 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" <> schrieb im
    > Newsbeitrag news:...
    >
    >> 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
    >>>
    David R. Longnecker, Sep 20, 2007
    #5
  6. Ahh, okay. Then ignore my trial and error and code. Glad you got it working!

    -dl

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

    > 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" <> schrieb im
    > Newsbeitrag news:...
    >
    >> 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
    >>>
    David R. Longnecker, Sep 20, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RTM
    Replies:
    4
    Views:
    2,735
    mikeb
    Jan 20, 2004
  2. Nathan Sokalski

    Determining What Control Caused The PostBack

    Nathan Sokalski, Sep 5, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    930
    Nathan Sokalski
    Sep 5, 2005
  3. =?Utf-8?B?ZGNoaWxsbWFu?=

    Determining control that caused a postback

    =?Utf-8?B?ZGNoaWxsbWFu?=, Oct 18, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    4,336
    =?Utf-8?B?ZGNoaWxsbWFu?=
    Oct 19, 2005
  4. Nathan Sokalski

    Determining What Control Caused The PostBack

    Nathan Sokalski, Sep 5, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    4
    Views:
    128
    Nathan Sokalski
    Sep 5, 2005
  5. Nathan Sokalski

    Determining What Control Caused The PostBack

    Nathan Sokalski, Sep 5, 2005, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    151
    Nathan Sokalski
    Sep 5, 2005
Loading...

Share This Page