0
votes

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.

1
did your server side event work when you remove the OnClientClick event? also could you try updating clientclickevent to "return Confirm()"ssilas777
One rule is important: No mater how much they look like Desktop applications, ASP.Net pages are still just 1980 HTML WebFormulars! All the old rules and limits of that technology still apply. The Server side click handler would be part of the normal processing on a postback.The purpose of JavaScript on the client side (in particular as part of AJAX) is to not get to a postback, or at least not a full postback. Or to display stuff better then waht 1980 HTML supports.Christopher

1 Answers

2
votes

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.