Do something javascript before asp.net anypostback

2019-03-18 20:52发布

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... :(

4条回答
相关推荐>>
2楼-- · 2019-03-18 21:29

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>
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-03-18 21:34

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" ... />
查看更多
Luminary・发光体
4楼-- · 2019-03-18 21:39

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.

查看更多
男人必须洒脱
5楼-- · 2019-03-18 21:41

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);
查看更多
登录 后发表回答