先写了如下方法
protected void Page_Load(object sender, EventArgs e)
{
Button LB = new Button();
LB.Text = "测试";
//DIV1是页面上一个DIV
DIV1.Controls.Add(LB);
LB.Click += new EventHandler(Test);
}
protected void Test(object sender, EventArgs e)
{
Response.Write("<script>alert('This is a test');</script>");
}
但上面的方法没有传传递参数,只能调用一个方法,当然用极端方法可以把要传的参数赋给按钮的ID或什么属性的带过去,但不是最好的方法,于是又找到如下方法:
protected void Page_Load(object sender, EventArgs e)
{
Button LB = new Button();
LB.Text = "测试";
LB.CommandName = "1";
LB.CommandArgument = "5";
DIV1.Controls.Add(LB);
LB.Command += new CommandEventHandler(this.Test);
}
protected void Test(object sender, CommandEventArgs e)
{
Response.Write("<script>alert('"+e.CommandArgument+"|"+e.CommandName+"');</script>");
}
这样就可以通过按钮的CommandName 和CommandArgument 进行参数传递了。