按钮单击事件不会更改

时间:2016-04-10 08:25:53

标签: c# asp.net

我需要一些帮助,在这段代码中出现了什么问题

protected void Page_Load(object sender, EventArgs e)
{ }

protected void Button1_Click(object sender, EventArgs e)
{
   Button1.ID = "Button4";
   Button2.ID = "Button1";
   Button1.Click -= Button1_Click;
   Button1.Click +=new EventHandler( Button2_Click);
   //Button2.ID = "Button1";
}

protected void Button2_Click(object sender, EventArgs e)
{
    Response.Write("success"); 
}

当我点击button1时,它会调用button1_click,当我点击button2时,它也会调用button1_click而不是button2_click。

1 个答案:

答案 0 :(得分:0)

我相信这可能是你的问题:

Button2.ID = "Button1";

根据MSDN Documentation设置Control.ID“为您提供对服务器控件属性,事件和方法的编程访问权。” 。您似乎将Button1分配给Button2,使Button2触发Button1订阅的事件。

删除Button2.ID行,我认为它应该有用。

如果它仍然无效,我建议您查看.Designer.cs文件,看看您是否订阅了Button2_Click事件。

相关问题