views:

75

answers:

4

Hi All,

i have a regular asp:button. I am working in .Net 3.5. I've tried adding a js confirm to the button with the OnClientClick attribute as well as adding in code-behind and the result is the same. No matter what the user clicks in the confirm pop-up the form will not submit??

BtnDeleteSelected.Attributes.Add("onclick", "return confirm('Are you sure you want to delete?');");

The confirm dialog appears and if i select "OK" it still does not submit.. Any ideas? thanks.

A: 

Do you have to return TRUE if validation passes? Meaning, if confirm() returns false I don't think the form will submit.

n8wrl
from what i understand the above is all that is needed.. clicking ok returns true
rodrick
A: 

Your code looks OK on the surface. Do you have any validators that might be preventing it being submitted? Try adding BtnDeleteSelected.CausesValidation = false to prevent the delete button calling any client-side validators.

Dan Diplo
i've added BtnDeleteSelected.CausesValidation = false thinking the same thing.. no dice.. there is no other validation on the page.
rodrick
Although i am using radgrid with confirms in there.. probably the issue.. but there are no js errors.
rodrick
A: 

Please try calling doPostBack function ;)

I mean:

BtnDeleteSelected.Attributes.Add("onclick", "CheckConfirm();");

<script language="javascript">
function CheckConfirm()
{
    if ( confirm('Are you sure you want to delete?') )
        __doPostBack('BtnDeleteSelected','');
    else
        return false;

    return true;
}
</script>

Hope that helps,

Ramon Araujo
+3  A: 

That's because you should not be returning in all cases. If you view the source of the page and look for the button markup, you are likely to see this...

onclick="return confirm('Ok?'); __doPostback();"

The __doPostback invocation is inserted automatically by the ASP.NET framework in some cases. Since you return immediately regardless of the result of confirm, the postback never fires. The ultimate solution would be to not to set the onclick attribute, but instead use an unobtrusive approach. However, if there is pressing reason to control this on the server-side via onclick, you can use a simple if statement and only return when they press "Cancel"...

string js = "if(!confirm('Are you sure you want to delete?')) return false;";
BtnDeleteSelected.Attributes.Add("onclick", js);
Josh Stodola
great thanks! Makes perfect sense. The only pressing need is probably bad habit mixed with laziness.. something i should overcome i know! Thanks again.
rodrick