Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Ajax: Identify the control that caused the Async Postback

4.33/5 (5 votes)
12 Jan 2010CPOL 38.7K  
There will be some scenarios where we may have to identify the sender that caused the async postback(While using update panel) . Normally we will try to identify the control by checking the sender element . This wont be always giving the correct value as multiple clicks/ elements under the...
There will be some scenarios where we may have to identify the sender that caused the async postback(While using update panel) . Normally we will try to identify the control by checking the sender element . This wont be always giving the correct value as multiple clicks/ elements under the update panel can alter this. We can identify the control that caused an async postback by sending the ID of the control along with the request as user context. You can use the following script for this.

<script>
	Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
	Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
        
	function BeginRequestHandler(sender, args) {
            try {
                args.get_request().set_userContext(args.get_postBackElement().id);
            }
            catch (e) {
            }
        }

        function EndRequestHandler(sender, args) {
            try {
                if (args.get_error() == undefined) {
                    var sName = args.get_response().get_webRequest().get_userContext();
                    if (sName == "btnDetails") {
                        //DoSomething();
                    }
                    else {
                        //DoSomethingelse();
                    }
                }
            }
            catch (e) {
            }
        }

</script>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)