Adding event handler for dynamically created controls

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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
 
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
 
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
 
Back
Top