How to submit ASP.NET with JavaScript after custom validation

前端 未结 3 1930
一生所求
一生所求 2020-12-16 02:16

I need to fire some custom JavaScript validation and then submit my ASP.NET using JavaScript.

How do I submit the form using JavaScript?

3条回答
  •  萌比男神i
    2020-12-16 02:58

    To do a postback via JavaScript you can call the following server side to create the JavaScript code for you:

    string postBackJavascript = Page.GetPostBackEventReference(yourControl);
    

    This will return the __doPostBack JavaScript code as a string, and you will need place it on your page attached to something or you can call the __doPostBack directly on your own with:

    __doPostBack(yourControlId,'');
    

    If you're doing it yourself and not using Page.GetPostBackEventReference then make sure to get the ClientID for the control that triggered the validation, like:

    __doPostBack('<%= yourControl.ClientID %>','');
    

    EDIT: After re-reading your question you didn't say you wanted to trigger the postback based on an ASP.NET control, you might not even be using any ASP.NET controls so in that case if you want to just do a vanilla postback you can do:

    document.forms[0].submit();
    

提交回复
热议问题