How to intercept any postback in a page? - ASP.NET

拟墨画扇 提交于 2019-12-17 16:09:49

问题


I want to intercept any postbacks in the current page BEFORE it occurs . I want to do some custom manipulation before a postback is served. Any ideas how to do that?


回答1:


There's a couple of things you can do to intercept a postback on the client.

The __doPostBack function looks like this:

function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}

Notice that it calls "theForm.onsubmit()" before actually doing the postback. This means that if you assign your form an onsubmit javascript function, it will always be called before every postback.

<form id="form1" runat="server" onsubmit="return myFunction()">

Alternately, you can actually override the __doPostBack function and replace it with your own. This is an old trick that was used back in ASP.Net 1.0 days.

var __original= __doPostBack;
__doPostBack = myFunction();

This replaces the __doPostBack function with your own, and you can call the original from your new one.




回答2:


Use the following options

All options works with ajax-enabled-forms and simple forms.
return false to cancel submit within any submit-handler.

Page.ClientScript.RegisterOnSubmitStatement(Page.GetType(), "submit-handler", "alert(\"On PostBack\");");

Equivalent javascript --don't use this code with previous code simultaneously

// Modify your form tag like this
<form onsubmit="javascript:return submit_handler();" ...>

// Add this script tag within head tag
<script type="text/javascript">
    function submit_handler() {
        // your javascript codes
        // return false to cancel
        return true; // it's really important to return true if you don't want to cancel
    }
</script>

And if you want complete control over __doPostBack put this script next to your form tag

<script type="text/javascript">
    var default__doPostBack;
    default__doPostBack = __doPostBack;
    __doPostBack = function (eventTarget, eventArgument) {
        // your javascript codes
        alert('Bye __doPostBack');
        default__doPostBack.call(this, eventTarget, eventArgument);
    }
</script>

Tested with ASP.NET 4.0




回答3:


To get the postback before a page does, you can create an HttpHandler and implement the ProcessRequest function.

Check this Scott Hanselman link for a good blog post on how to do it (including sample code).




回答4:


Page.IsPostBack is your friend.




回答5:


You can check for a postback in one of the page events for your form.

If you want to take some action on postback that involves creating controls or manipulating viewstate then you may want to come in on an earlier event like Page_Init.

Try this:

protected void Page_Init(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            //Check for your conditions here, 

            if (Page.IsAsync)
            {
                //also you may want to handle Async callbacks too:
            }
        }
    }



回答6:


not sure, but I think you are looking for..

if (Page.IsPostBack)
    { 
    }


来源:https://stackoverflow.com/questions/2234542/how-to-intercept-any-postback-in-a-page-asp-net

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