ASP.NET Validation Controls and Javascript Validation Windows

I have a page using .NET server side input validation elements. This page also has a javascript confirmation window that launches when the form is submitted. Currently, when the Submit button is selected, a javascript confirmation window appears, and as soon as it is confirmed, ASP.NET server-side validation elements are activated. I would like to run control checks on the server side before the javascript confirmation window displays.

How can I do that? Ive included a sample of my current code below.

Sample.aspx

<asp:textbox id=foo runat=server />
<asp:requiredfieldvalidator id=val runat=server controltovalidate=foo />
<asp:button id=submit runat=server onClientClick=return confirm('Confirm this submission?') />

sample.aspx.vb

Sub Page_Load()
    If Page.IsPostback() Then
        Page.Validate()

        If Page.IsValid Then
            'process page here'
        End If
    End If
End Sub

Thanks for any help.

+3
source share
8 answers

.

:

, confirm, . - submit onclick.

, ( ), confirm, .

: :

ASP.NET , onClick . OnClick OnClick. , , :

  • add CausesValidation = False
  • Validate() IsValid onClick , .

2:

<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="if (Page_ClientValidate()){ return confirm('Do you want to submit this page?')}" CausesValidation="false" />
+7

     <asp:Button ID="btnSave" runat="server" OnClientClick="javascript:return ConfirmSubmit()" OnClick="btnSave_Click" Text="Save" /> 


//---javascript -----
function ConfirmSubmit()
{
   Page_ClientValidate();
   if(Page_IsValid) {
       return confirm('Are you sure?');
    }
 return Page_IsValid;
}
+2

EnableClientScript , , ?

+1

, javascript . ..

, , JS , .

MAY , JS HIJAX, onClick , , , .

+1

onsubmit .

. , , JS.

+1

ASP.NET Control Toolkit ValidatorCallout? : http://www.asp.net/AJAX/AjaxControlToolkit/Samples/ValidatorCallout/ValidatorCallout.aspx

ValidatorCallout - ASP.NET AJAX, ASP.NET. , , . ValidatorCallout TargetControlID .

, , .

+1

.

function validate()
{
    Page_ClientValidate();
    if (Page_IsValid)
        // do your processing here

    return Page_IsValid;
}

This method can be called in the "onClientClick" event of the button and in the code, you can, if the page is valid and performs processing, if the client-side check was successful.

So, in the button click event you can do -

protected void SubmitButton_Click(object sender, EventArgs e)    
{    
    if (!this.isValid)
        return;

    // do the processing here
}
0
source

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


All Articles