我想以编程方式创建一个按钮,当我点击该按钮时,我想调用它的事件处理程序....这是我的代码.......我做错了什么?
protected void DashboardDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
Button objbutton = new Button();
objbutton.ID = "objbutton";
objbutton.Text = "Hello";
form1.Controls.Add(objbutton);
Page.Controls.Add(form1);
objbutton.Click += new System.EventHandler(objbutton_Click);
}
protected void objbutton_Click(object sender, EventArgs e)
{
Response.Write("Hello World");
}
如果我点击 objbutton ,则 DashboardDropDownList_SelectedIndexChanged 事件会被触发而不是objbutton_Click
答案 0 :(得分:0)
像这样使用
protected void DashboardDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
Button objbutton = new Button();
objbutton.ID = "objbutton";
objbutton.Click += new System.EventHandler(objbutton_Click);
objbutton.Text = "Hello";
form1.Controls.Add(objbutton);
}
protected void objbutton_Click(object sender, EventArgs e)
{
Response.Write("Hello World");
}