Web - Buttons In A DataGrid or DataList

  • Thread starter Thread starter David P. Donahue
  • Start date Start date
D

David P. Donahue

If I have a DataGrid or DataList on a webpage with an ItemTemplate that
includes a button server control, how can I set it in the ItemTemplate
so that all the buttons populated in the grid/list will call a specified
function in my code-behind. Also, from that function, how can I access
the properties of the button that called it?


Regards,
David P. Donahue
(e-mail address removed)
 
Create the event handler for your button as follows, take note that the
accessibility of the method is set to protected and not to the usual private
setting.

protected void ButtonClick(object sender, System.EventArgs e)

{

Response.Write("Button: " + ((Button)sender).ClientID + " was clicked!");

}

In your aspx file, where you declare the button in the item template,
include this in the button declaration tag:

ONCLICK="ButtonClick"


The ClientID property will return a value like "_ctl1_btn1" for the first
button, "_ctl2_btn1" for the second, and so on.

HTH

DalePres
MCAD, MCDBA, MCSE
 
Perfect :) Casting the "sender" object to an ImageButton was precisely
what I needed to do. Thanks!


Regards,
David P. Donahue
(e-mail address removed)
 
I didn't even notice that both these threads were by the same person. I
just figured a coincidence that I was sending basically the same code twice
in one night. LOL

DalePres
 
Back
Top