Web - Buttons In A DataGrid or DataList

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)
 
D

DalePres

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
 
D

David P. Donahue

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


Regards,
David P. Donahue
(e-mail address removed)
 
D

DalePres

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top