void BindButtons() { foreach (var item in items)
{
Button Btn = new Button();
Btn.OnClientClick = "showLoading();return true;"; //客户端事件
//Btn.Attributes.Add("OnClientClick ", "this.disabled=true;return true;"); //无效
//Btn.UseSubmitBehavior = false;/*默认为true时按钮的type=submit会触发表单提交;当为false时生成的html里按钮的type=button是通过js执行__doPostBack(...)来提交表单,为false时OnClientClick里的函数必须return true才能提交form*/
Btn.Width = 180;
Btn.Height = 180;
Btn.Style.Add("margin", "10px");
Btn.ID = item.UUId;
Btn.Text = item.OrgName;
Btn.Click += new EventHandler(Btn_Click);
this.div1.Controls.Add(Btn);
} }
void Btn_Click(object sender, EventArgs e)
{
var btn = sender as Button;
Utils.JSExecutor.Execute(this,"alert('"+btn.Id+"')");
}
2.调用:
protected void Page_Load(object sender, EventArgs e)
{
BindButtons();/*注意动态创建button的位置不能放在下面的if里否则服务端事件不能执行*/
if (!IsPostBack)
{
BindData();
}
}
From:http://www.cnblogs.com/xuejianxiyang/p/5549697.html
|
请发表评论