日期:2014-05-17  浏览次数:20422 次

怎么动态的为Button添加单击事件
我在GridView的脚注中添加了一个Button按键,但没反应

GridView2.FooterRow.Cells.Clear();

TableCell tc = new TableCell();

Literal lite = new Literal();
lite.Text = string.Format("合计:<span style='color:Red;'>{0}</span>元", money.ToString());

Button bt_Pay = new Button();
bt_Pay.Text = "付款";
//定义事件
bt_Pay.OnClientClick = "bt_Pay_Click";

tc.Controls.Add(lite);
tc.Controls.Add(bt_Pay);
GridView2.FooterRow.Cells.Add(tc);

//Button的单击事件
protected void bt_Pay_Click(object sender, EventArgs e)
{
  WebMessageBox.AlertMsg("你好!");
  Response.Write("你好囝");
}

仔细点,或给个动态添加Button按键事件的源码地址

------解决方案--------------------
因为你这个是在gridview databound绑定的,你一定加了if(!IsPostBack)

所以在PostBack的时候是点击当然不会起作用了,你把绑定gridview提到if(!IsPostBack) 外面
------解决方案--------------------
参考:
http://www.cnblogs.com/insus/articles/2064359.html