Click here to Skip to main content
15,881,757 members
Articles / Web Development / ASP.NET
Tip/Trick

Ajax: Identify the control that caused the Async Postback

Rate me:
Please Sign up or sign in to vote.
4.33/5 (5 votes)
12 Jan 2010CPOL 37.8K   6   3
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)


Written By
Software Developer (Senior) NA
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWant some explanation. Pin
Mayur Asalmol2-Aug-16 0:58
Mayur Asalmol2-Aug-16 0:58 
GeneralThanks, I really don't need it yet but I am sure this is goi... Pin
Azerax13-Sep-11 23:18
Azerax13-Sep-11 23:18 
GeneralNice one... thanks. Pin
Ravishankar Visvan27-Jul-11 3:10
Ravishankar Visvan27-Jul-11 3:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.