后台动态添加的button,如何触发button_click事件?

后台动态添加的button,需要在Page_Load或者Page_Init重新动态生成才能执行button_click

 

public Panel GetContrlType()
{
Panel panel = new Panel();
Button btnsearch = new Button();

btnsearch.Text = "查询";
btnsearch.ID = "btnsearch";
btnsearch.Click += new EventHandler(btnseach_Click);
panel.Controls.Add(btnsearch);
}
return panel;
}

 

protected void Page_Load(object sender, EventArgs e)
{
try
{
Button bsearch = PanelWarp.FindControl("btnsearch") as Button;
(Panel.FindControl("panel") as Panel).Controls.Add(bsearch);
bsearch.Click += new EventHandler(bsearch_Click);
}
catch (Exception ex)
{
throw ex;
}
}

 

 

protected void bsearch_Click(object sender,EventArgs e)
{

}

 而且在Page_Load若使用if (!IsPostBack),重新加载页面后动态生成的控件会消失

posted @ 2016-11-15 14:02  雪墨ing  阅读(3139)  评论(0编辑  收藏  举报