Adding event handler for dynamically created controls

Discussion in 'ASP .Net' started by =?Utf-8?B?QW5kcmV3IFdpZWQ=?=, Apr 19, 2004.

  1. Hello
    Please excuse me if this come in as a duplicate.

    Here is the situation that I am having: I am pulling data from a database and writing it out as a list. Next to each item that I write, I want to make a linkbutton control which will allow you to invalidate the database record. Since I don't know how many records will be present, I need to have all of the linkbuttons access the same event. The trick is to know WHICH linkbutton was pressed. Is there a way that anyone can suggest to accomplish this? I have been trying to do this for a good deal of time so far
    Thank you
    Andre
    =?Utf-8?B?QW5kcmV3IFdpZWQ=?=, Apr 19, 2004
    #1
    1. Advertising

  2. Andrew, I presume you are writing it out as a datalist. No matter which button the user clicks in the datalist, the ItemCommand event fires. Inside ItemCommand, e.item is the datalistitem of the row that was clicked, and in e.item.controls you'll find the data that's shown in the grid. In addition to or instead of, you can bind the linkbutton's CommandArgument property to your primary key field, then retrieve CommandArgument in ItemCommand and use it to go get your data from somewhere, etc. (this is handy when you want a key or something that you're not showing in the grid, although you can do that too with hidden fields). ht

    Bil

    ----- Andrew Wied wrote: ----

    Hello
    Please excuse me if this come in as a duplicate.

    Here is the situation that I am having: I am pulling data from a database and writing it out as a list. Next to each item that I write, I want to make a linkbutton control which will allow you to invalidate the database record. Since I don't know how many records will be present, I need to have all of the linkbuttons access the same event. The trick is to know WHICH linkbutton was pressed. Is there a way that anyone can suggest to accomplish this? I have been trying to do this for a good deal of time so far
    Thank you
    Andre
    =?Utf-8?B?QmlsbCBCb3Jn?=, Apr 19, 2004
    #2
    1. Advertising

  3. Thank you for the reply
    Actually, I'm not using a datalist. Basically, I am wondering if there is a way to get two or more (a variable quantity) of controls[linkbuttons, specifically] to fire the same event, and have the event handler know which one threw the event

    Is there a way that anyone knows

    Thanks
    Andre

    ----- Bill Borg wrote: ----

    Andrew, I presume you are writing it out as a datalist. No matter which button the user clicks in the datalist, the ItemCommand event fires. Inside ItemCommand, e.item is the datalistitem of the row that was clicked, and in e.item.controls you'll find the data that's shown in the grid. In addition to or instead of, you can bind the linkbutton's CommandArgument property to your primary key field, then retrieve CommandArgument in ItemCommand and use it to go get your data from somewhere, etc. (this is handy when you want a key or something that you're not showing in the grid, although you can do that too with hidden fields). ht

    Bil
    =?Utf-8?B?QW5kcmV3IFdpZWQ=?=, Apr 20, 2004
    #3
  4. Just attach the same handler to the event you want from each of the controls, then in the handler use the "sender" object to figure out who sent it--e.g. you've got a bunch of buttons, you attach MyButtonHandler to each button's Click event, and then in MyButtonHandler you do something like (button)sender.name to figure out who was clicked. Does that help

    ----- Andrew Wied wrote: ----

    Thank you for the reply
    Actually, I'm not using a datalist. Basically, I am wondering if there is a way to get two or more (a variable quantity) of controls[linkbuttons, specifically] to fire the same event, and have the event handler know which one threw the event

    Is there a way that anyone knows

    Thanks
    Andre

    ----- Bill Borg wrote: ----

    Andrew, I presume you are writing it out as a datalist. No matter which button the user clicks in the datalist, the ItemCommand event fires. Inside ItemCommand, e.item is the datalistitem of the row that was clicked, and in e.item.controls you'll find the data that's shown in the grid. In addition to or instead of, you can bind the linkbutton's CommandArgument property to your primary key field, then retrieve CommandArgument in ItemCommand and use it to go get your data from somewhere, etc. (this is handy when you want a key or something that you're not showing in the grid, although you can do that too with hidden fields). ht

    Bil
    =?Utf-8?B?QmlsbCBCb3Jn?=, Apr 25, 2004
    #4
    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. Replies:
    1
    Views:
    695
    Damien
    Feb 22, 2007
  2. gnewsgroup
    Replies:
    4
    Views:
    671
    gnewsgroup
    Feb 19, 2008
  3. msimmons
    Replies:
    0
    Views:
    472
    msimmons
    Jul 16, 2009
  4. John Kilgo

    Adding Event Handler to Dynamically added Controls

    John Kilgo, Apr 25, 2005, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    202
    John Kilgo
    Apr 26, 2005
  5. Adi
    Replies:
    2
    Views:
    191
Loading...

Share This Page