动态添加了N个按钮,按钮能够正常添加,可是在给按钮注册事件的时候没效果了,需要的效果是点击按钮跳转。帮忙看下哪里错了,谢谢。 //添加按钮
private void AddBtn(string txt, string id, string type)
{
Button btn = new Button();
btn.ID = "btn" + tr.Controls.Count.ToString();
btn.CommandArgument = id;
btn.Text = txt;
btn.CssClass = "btncss";
if (type == "1")
btn.Click += new EventHandler(btn1_Click); //2种方法都不行
// btn.Attributes.Add("onclick", "javascript:location='http://www.baidu.com';"); //
else if (type == "2")
//btn.Click += new EventHandler(btn2_Click);
btn.Attributes.Add("onclick", "<script>alert('2')</script>");
else if (type == "3")
// btn.Click += new EventHandler(btn3_Click);
btn.Attributes.Add("onclick", "<script>alert('3')</script>");
tr.Controls.Add(btn);
}
public void btn1_Click(object sender, EventArgs e)
{
Response.Redirect("http://www.baidu.com");
}
private void AddBtn(string txt, string id, string type)
{
Button btn = new Button();
btn.ID = "btn" + tr.Controls.Count.ToString();
btn.CommandArgument = id;
btn.Text = txt;
btn.CssClass = "btncss";
if (type == "1")
btn.Click += new EventHandler(btn1_Click); //2种方法都不行
// btn.Attributes.Add("onclick", "javascript:location='http://www.baidu.com';"); //
else if (type == "2")
//btn.Click += new EventHandler(btn2_Click);
btn.Attributes.Add("onclick", "<script>alert('2')</script>");
else if (type == "3")
// btn.Click += new EventHandler(btn3_Click);
btn.Attributes.Add("onclick", "<script>alert('3')</script>");
tr.Controls.Add(btn);
}
public void btn1_Click(object sender, EventArgs e)
{
Response.Redirect("http://www.baidu.com");
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货