Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get javascript confirm box after validating the form data in asp.net?

I am facing a problem in my asp.net web application that i need to display a java script confirmation box on click of Update button and this as per requirements given to me.

Currently I am using onclient click

<asp:Button ID="btnCustomerUpdate" runat="server" CssClass="applybuttonstyle" 
            Text="Update" onclick="btnCustomerUpdate_Click" 
            OnClientClick="return confirm('Do you really want to update?');"/>

It's working fine but the problem is i also applied some validation controls on my form fields so if a user leave any field blank and click update button then it shows javascript confirm box and the validation message below that form field.

And it is not desired behavior it should not show the confirmation box until unless all validation are passed.

I hope this is due to because i am using this on client click so please tell me a better solution for that.

Thanks in advance.

like image 257
Peeyush Avatar asked Dec 21 '22 04:12

Peeyush


1 Answers

you can use Page_ClientValidate in your own confirm function to decide whether to display the confirmation dialog. Something like:

function validateAndConfirm(message){
   var validated = Page_ClientValidate('group1');
   if (validated){
      return confirm(message); 
   }
}

And on the server you will have something like:

 <asp:textbox id="TextBox1" runat="server"/>
      <asp:requiredfieldvalidator ValidationGroup="group1"
                                                   ErrorText="Need to Fill in Value!"
                                                   ControlToValidate="TextBox1"
                                                   runat="server"/>

  <asp:textbox id="TextBox2" runat="server"/>
       <asp:requiredfieldvalidator ValidationGroup="group1"
                                                     ErrorText="Need to Fill in Value!"
                                                     ControlToValidate="TextBox2"
                                                     runat="server"/>

And your button code will change to:

<asp:Button ID="btnCustomerUpdate" runat="server" CssClass="applybuttonstyle" 
        Text="Update" onclick="btnCustomerUpdate_Click" ValidationGroup="group1"
        OnClientClick="return validateAndConfirm('Do you really want to update?');"/>

Obviously, I cannot test this, but you get the idea.

If you will be using Page_ClientValidate you might find this SO question useful.

like image 123
Pencho Ilchev Avatar answered Dec 24 '22 01:12

Pencho Ilchev