Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 回发后未触发按钮单击事件_C#_Asp.net - Fatal编程技术网

C# 回发后未触发按钮单击事件

C# 回发后未触发按钮单击事件,c#,asp.net,C#,Asp.net,我在我的webform上的按钮点击事件中定义了以下代码。我第一次单击按钮时,代码正确执行。额外的点击应该会给我的数据库添加更多的值,但是在这种情况下,在第一次回发之后,点击事件不会触发 当我离开页面并返回时,我可以在列表中再添加一个名字,但随后的单击仍然不起作用。我在这一点上被难住了…没有在页面上抛出任何Javascript错误(这是我想到的第一件事) 如果正在动态创建有问题的按钮,请确保在每次回发时重新附加Click事件处理程序。听起来这可能就是问题所在 最好的方法是在页面的内部创建按钮控件,

我在我的webform上的按钮点击事件中定义了以下代码。我第一次单击按钮时,代码正确执行。额外的点击应该会给我的数据库添加更多的值,但是在这种情况下,在第一次回发之后,点击事件不会触发

当我离开页面并返回时,我可以在列表中再添加一个名字,但随后的单击仍然不起作用。我在这一点上被难住了…没有在页面上抛出任何Javascript错误(这是我想到的第一件事)


如果正在动态创建有问题的按钮,请确保在每次回发时重新附加Click事件处理程序。听起来这可能就是问题所在

最好的方法是在页面的内部创建按钮控件,并将事件处理程序附加到那里。这样,它就是ViewState的一部分,页面生命周期中的后续事件将了解它

Page_PreInit(Object sender, EventArgs e)
{
    Button yourBtn = new Button();
    // Set your other properties
    yourBtn.Click += yourEventHandlerName;
    // Add the control to the controls collection of whatever container it belongs in
    yourContainer.Controls.Add(yourBtn);
}

最后,通过将CausesValidation=“False”添加到此特定按钮来解决此问题。

您是否已调试以检查您的程序是否输入了“受保护的void your_Handler(object sender,EventArgs e)”部分?您能否向我们展示按钮和周围的html元素?
Page_PreInit(Object sender, EventArgs e)
{
    Button yourBtn = new Button();
    // Set your other properties
    yourBtn.Click += yourEventHandlerName;
    // Add the control to the controls collection of whatever container it belongs in
    yourContainer.Controls.Add(yourBtn);
}