简体   繁体   中英

OnClick Event Not Firing on Button Click ASP.NET

I have an asp:Button that is in a JavaScript dialog window. It has an OnClick event called DialogWindowButton_Click as you can see in the code below. The event is not firing and I have put breakpoints in the C# file and it is not even entering the function. I'm not sure why and have looked at other forum posts to try to figure this out. I have 1) deleted the button and recreated the button and OnClick event themselves (this didn't work), and 2) added CausesValidation="False" to the asp:Button tag. Neither avenue has worked. What I have is shown below:

<div style="margin:auto; width:100px; padding-bottom:15px;">
    <asp:Button ID="DialogWindowButton" runat="server" Text="Save Entry" 
    OnClick="DialogWindowButton_Click" CausesValidation="False"/>
</div>

Then in the C# file, I have:

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

    }

    protected void DialogWindowButton_Click(object sender, EventArgs e)
    {
        DialogWindowButton_ClickHelper();
        ...
    }

    protected AddressBookEntry DialogWindowButton_ClickHelper()
    {
        ...
    }
    ...

I have the correct file for the CodeBehind tag as well as for the Inherits tag. In the C# file you can see that the original OnClick event calls on a helper function defined directly below it, but breakpoints in the top of DialogWindowButton_Click() aren't being reached. There are no build errors either. Could there be something else I'm missing? Thank you!

You need to set UseSubmitBehavior to false (default is true):

 <asp:Button ID="DialogWindowButton" runat="server" Text="Save Entry" 
    UseSubmitBehavior="False" OnClick="DialogWindowButton_Click" CausesValidation="False" />

From Reference :

Use the UseSubmitBehavior property to specify whether a Button control uses the client browser's submit mechanism or the ASP.NET postback mechanism. By default the value of this property is true, causing the Button control to use the browser's submit mechanism. If you specify false, the ASP.NET page framework adds client-side script to the page to post the form to the server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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