Click here to Skip to main content
15,913,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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??
Posted
Updated 14-Oct-11 9:37am
v2

 
Share this answer
 
foreach (GridViewRow rowItem in GridView1.Rows)
{
    // why are you never using the 'rowItem variable ?
    Button btnMonday = (Button)(GridView1.Rows[0].Cells[1].FindControl("btnMonday"));
    btnMonday.Click += new EventHandler(this.mondae_Click);
 
    // why is the Button in Cell[2] named 'btnMonday' ?
    Button btnTuesday = (Button)(GridView1.Rows[0].Cells[2].FindControl("btnMonday"));
    btnTuesday.Click += new EventHandler(this.mondae_Click);
}

Because you do not use the 'rowItem variable in the loop, your code is now setting the same two buttons in Row[0] to have multiple copies of the same Event handler.
 
Share this answer
 
Comments
Anele Ngqandu 14-Oct-11 19:21pm    
how to use the rowItem
BillWoodruff 14-Oct-11 19:30pm    
I think it's very important for you to understand what the GridView Rows collection is. And you need to focus on that.
for each button in the command name add different value
& in the design make the click event button for the all buttons is the same event

then in the event click try this code

switch (((button)sender).commandname)
{
case your value
execute code
break;
case your other value
execute code
break;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900