4

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.

competent_tech
  • 44,465
  • 11
  • 90
  • 113
Peeyush
  • 4,728
  • 16
  • 64
  • 92

3 Answers3

10

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.

Community
  • 1
  • 1
Pencho Ilchev
  • 3,201
  • 18
  • 21
  • thanks for the answer i will try to impletment it soon then if it will work then i will mark you accepted:-) – Peeyush Dec 27 '11 at 10:34
1

You can call the Page_ClientValidate method to ensure the page is valid before asking the user:

if (typeof(Page_ClientValidate) == 'function' && Page_ClientValidate() == false) {
    return false;
} else {
    return confirm('Do you really want to update?');
}
competent_tech
  • 44,465
  • 11
  • 90
  • 113
0

You should atleast apply validation on client side first and than open a confirmation dialog, while on server, you have to use server side validation first too in case if user has disabled javascript, and than update the record.

You can also use AJAX to update record, so if a validation message occurs, shows error message and if the validation passed, than you can alert the user but the only disadvantage of using that is you have to re-post the data again. To mitigate this problem you either have to save in temporary table, until user gave confirmation and than delete it from temporary table but obviously it takes a lot of work.

Adeel
  • 19,075
  • 4
  • 46
  • 60