单击gridview中的特定按钮时执行事件 [英] Execute an event when a specific button inside a gridview is clicked

查看:74
本文介绍了单击gridview中的特定按钮时执行事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在gridview单元格中有一个按钮列表,现在我需要每个按钮在单击时执行不同的事件.我该如何解决?

我试图将其放在Page_Load上,但不起作用

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??

推荐答案


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);
}


因为您没有在循环中使用''rowItem变量,所以您的代码现在在Row [0]中设置相同的两个按钮,以具有同一事件处理程序的多个副本.


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.


命令名称中的每个按钮添加不同的值
&在设计中,使所有按钮的单击事件按钮都是同一事件

然后在发生事件的情况下,单击尝试此代码

开关((((button)sender).commandname)
{
重视您的价值
执行代码
休息;
列出您的其他值
执行代码
休息;
}
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;
}


这篇关于单击gridview中的特定按钮时执行事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆