How to get javascript confirmation window after validating form data in asp.net?

I am having a problem in my asp.net web application that I need to display a java script confirmation window when I click Refresh , and this is in accordance with the requirements provided to me.

I am currently 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 works fine, but the problem is that I also applied some validation elements in my form fields, so if the user leaves the field and the empty refresh button, it shows a javascript confirmation window and a confirmation message below this form field.

And this is an undesirable behavior, it should not show a confirmation window until all the checks have passed.

I hope this is due to the fact that I use it on the client, so please tell me the best solution for this.

Thanks in advance.

+4
source share
3 answers

you can use Page_ClientValidate in your own confirmation function to decide whether to display a confirmation dialog. Sort of:

 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 can’t verify this, but you get the point.

If you will use Page_ClientValidate , you may find this SO question useful.

+9
source

You can call the Page_ClientValidate method to verify that the page is valid before a user request:

 if (typeof(Page_ClientValidate) == 'function' && Page_ClientValidate() == false) { return false; } else { return confirm('Do you really want to update?'); } 
+1
source

First, you must first apply client-side validation, and then open a confirmation dialog, while on the server, you must first use server-side validation if the user has disabled JavaScript and not updated the entry.

You can also use AJAX to update the record, therefore, if a verification message appears, an error message is displayed, and if the verification has passed, than you can warn the user, but the only drawback is that you need to re-publish the data again. To mitigate this problem, you either need to save the temporary table until the user confirms and removes it from the temporary table, but obviously it requires a lot of work.

0
source

Source: https://habr.com/ru/post/1388083/


All Articles