简体   繁体   English

ASP.NET Button事件未触发

[英]ASP.NET Button event is not firing

I have an asp:button event that's not firing. 我有一个不触发的asp:button事件。 Its weird because I have one exactly like it in the same page that is working perfectly, but this ones event is not firing when I click the button even though it exists, the page simply reloads without any of the code in the event executing. 这很奇怪,因为我在完全正常工作的同一页面中有一个完全类似于它的对象,但是当我单击按钮时,即使该事件存在,该事件也不会触发,该页面只是重新加载而没有执行事件中的任何代码。 I'd really appreciate some help. 我真的很感谢您的帮助。 Is it maybe because its in a bootstrap collapse? 可能是因为它在引导程序崩溃了吗?

Actually, now every time I add a button this happens, the old buttons still work, but I cant add any new ones, they just stopped working. 实际上,现在每次我添加按钮都会发生这种情况,旧按钮仍然可以使用,但是我无法添加任何新按钮,它们只是停止工作。

the .aspx file: .aspx文件:

    <asp:Button runat="server" ID="GuestCarSubmit" Text="Submit As Guest" OnClick="GuestCarSubmit_Click" />

the .aspx.cs file .aspx.cs文件

     protected void GuestCarSubmit_Click(object sender, EventArgs e)
    {
        MessageBox.Show("!!!!!!!!!!!");
        //this is not firing
    }

I can do GuestCarSubmit.UseSubmitBehavior = false; 我可以做GuestCarSubmit.UseSubmitBehavior = false; that would make the events fire, but also would make the textboxes empty on postback, and I also cant find a solution to that. 这将使事件触发,但也会使文本框在回发时为空,我也找不到解决方案。


Update: Ok to test I set the buttons UseSubmitBehavior to false. 更新:可以测试,我将按钮UseSubmitBehavior设置为false。 It turns out the button can read from another text box if submitBehavior is false but not from the one i need it to read from. 事实证明,如果submitBehavior为false,则可以从另一个文本框中读取按钮,但不能从需要读取的文本框中读取。 And if i set it to true it stops firing the event. 如果我将其设置为true,它将停止触发该事件。 (keep in mind my other buttons don't require UseSubmitBehavior to be false to fire their events, only this one); (请记住,我的其他按钮不需要UseSubmitBehavior为false来触发事件,仅此一个);


Update 2: After Messing around with the code, it seems anything in that specific form doesn't work, how do I fix that? 更新2:弄乱了代码之后,似乎该特定形式的任何内容均不起作用,如何解决该问题? all of its text disappears on postback and its buttons don't fire events, but the other form the bootstrap collapse works fine. 它的所有文本都会在回发时消失,并且其按钮不会触发事件,但是另一种形式的自举崩溃效果很好。

Try adding: 尝试添加:

OnClientClick="return false;"

Like: 喜欢:

<asp:Button runat="server" ID="GuestCarSubmit" Text="Submit As Guest" OnClick="GuestCarSubmit_Click" OnClientClick="return false;" />

In ASP.NET Web Form, you need to use RegisterStartupScript if you want to display a sample message box at client-side. 在ASP.NET Web窗体中,如果要在客户端显示示例消息框,则需要使用RegisterStartupScript

ClientScript.RegisterStartupScript(this.GetType(), "myfunc" + UniqueID, 
     "alert('hello!');", true);

请将原因验证属性添加到您的按钮中,如下所示。

Causes Validation = "false"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM