Do something javascript before asp.net anypostback

試著忘記壹切 提交于 2019-12-09 05:19:56

问题


I wanna do something in javascript before page goes for post back.

How to run a javascript function before any asp.net postback?

$('form').submit(function () {
      alert('hello');
});

It doesn't work... :(


回答1:


I find the way, in the asp.net forums and it was a some code in codebehind.

Just add this to your Page_Load event handler, changing the javaScript string to what you want to happen.

string scriptKey = "OnSubmitScript";
string javaScript = "alert('RegisterOnSubmitStatement fired');";
this.ClientScript.RegisterOnSubmitStatement(this.GetType(), scriptKey, javaScript);



回答2:


If you want to do something client-side before asp's postback, try using the OnClientClick attribute of the asp:button, eg:

<asp:Button OnClick="submit" OnClientClick="myPrePostbackFunction()" Text="Submit" runat="server" ... />



回答3:


try:

$('form').submit(function () {
    return confirm('you sure?');
});

The form won't be submitted unless you return true and before that you can do all you want. It doesn't have to be a confirm() call of course.




回答4:


try this?

    <form name="someform">
    <input type="submit" value="" />
</form>
<script type="text/javascript">
document.someform.onsubmit = function  ( e ) {
    e = e|| window.event;
    if ( !confirm ( "sure?") ){
        e.returnValue ? ( e.returnValue = false ):  e.preventDefault();
    }
}
// then jquery version
$("form").bind("submit", function  ( e ) {
    if ( !confirm ( "sure?" ) ){
        e.preventDefault();
    }
})
</script>


来源:https://stackoverflow.com/questions/9654808/do-something-javascript-before-asp-net-anypostback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!