2
Answers

Execute an event when a specific button inside a gridview is clicked

Photo of Anele Ngqandu

Anele Ngqandu

13y
1.3k
1
Hi
 
I have a list of buttons inside my gridview cells, now i need each button to execute a difrent event when its clicked. how can i solve this?
 
I tried to put this on Page_Load but not working
 
foreach (GridViewRow rowItem in GridView1.Rows)
                        {
Button btnMonday = (Button)(GridView1.Rows[0].Cells[1].FindControl("btnMonday"));
            btnMonday.Click += new EventHandler(this.mondae_Click);
 
Button btnTuesday = (Button)(GridView1.Rows[0].Cells[2].FindControl("btnMonday"));
            btnTuesday .Click += new EventHandler(this.mondae_Click);
 
                        }
 
What should i do or where should i put it??

Answers (2)

0
Photo of Prabhu Raja
NA 4.7k 1.5m 13y
Hi,

 i attached my source code with this. Instead of Command button, i have used link button. In code-behind, use link button Event handler , to do the task that you want.

In that code,  I retrieved grid row index based on link button click. so, based on row index, you can perform difference tasks.






0
Photo of Javeed M Shaikh
NA 7.8k 69.7k 13y
Hi Anele,

you have use the Rowcommand event of the grid like this:

protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "btnMonday")
{
// Retrieve the row index stored in the
// CommandArgument property.
int index = Convert.ToInt32(e.CommandArgument);

// Retrieve the row that contains the button
// from the Rows collection.
GridViewRow row = GridView1.Rows[index];

// Add code here
}

}

Please do not forget to mark "Accepted Answer".