简体   繁体   中英

OnClientClick not passing to OnClick code behind on return of true in my ASP.NET web solution

I have followed several solution suggestions for this problem but nothing is working properly. My desired outcome is to fire off a JavaScript validation function before executing the C# code behind method upon successful validation return of true. OnClick should be followed after the javascript OnClientClick operation.

I have tried suggestions from this thread:

Executing OnClick code behind method after returning true from OnClientClick javascript function

as well as this thread:

Executing OnClick code behind method after returning true from OnClientClick javascript function

This is my current layout:

<asp:Button ID="btnAdd" runat="server" AutoPostBack="true" OnClick="btnAdd_Click" OnClientClick="btnAdd_Click(this, event);" Text="Add" UseSubmitBehavior="false"/>&nbsp;



    function btnAdd_Click(sender, args) {
        if(confirm('Do you want to do a server trip?'))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

I have also tried:

<asp:Button ID="btnAdd" runat="server" AutoPostBack="true" OnClick="btnAdd_Click" OnClientClick="if (!Confirm()) return false;" Text="Add" UseSubmitBehavior="false"/>&nbsp;

function Confirm() {
    return confirm("Are you sure?");
}

What i am seeing is JavaScript is firing off every time. Debugging JS proves that it is returning true back to my solution, so the OnClick should get hit. I have a break point in my click event on the code behind but it never gets hit. Is there a site setting or something preventing this?

Help is much appreciated. Thanks in advance.

Try this...you really don't even need that JS function.

<asp:Button ID="btnAdd" runat="server" OnClientClick="return window.confirm('Are you sure?');" Text="Add" OnClick="btnAdd_Clicked"/>


protected void btnAdd_Clicked(object sender, EventArgs eventArgs)
{
    string test = "12345";
}

If you debug the btnAdd_Clicked function, you'll see it hits the first line of code there.

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