smarx.com

developing for the new web

How to handle a timeout in an async postback

Web service calls have an easy way to define a timeout handler. (After the actual web service method's parameters, it's the second parameter to the call.)

For async postbacks (UpdatePanels), it's not quite as simple. You need to handle the EndRequest event on the PageRequestManager. The first parameter to your event handler is a reference to the PageRequestManager itself, but the second parameter is an instance of the EndRequestEventArgs class. When an error occurs, you can use get_error() to take a look at what happened. Here's some code that uses an EndRequest event handler to check for a timeout error and handle it:

<%@ Page Language="C#" AutoEventWireup="true" %> 
 
<script runat="server"> 
    protected void slow_postback(object sender, EventArgs e) 
    { 
        // ten seconds is overkill... we timeout after one second (see ScriptManager below) 
        System.Threading.Thread.Sleep(10000); 
    } 
</script> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title>Handling an async postback timeout</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
        <!-- This sets the timeout on async postbacks (i.e. UpdatePanel refreshes) to one second --> 
        <asp:ScriptManager ID="ScriptManager1" runat="server" AsyncPostBackTimeout="1" /> 
        <asp:UpdatePanel runat="server"> 
            <ContentTemplate> 
                <asp:Button ID="Button1" runat="server" Text="Fast postback - no timeout" /> <br /> 
                <asp:Button ID="Button2" runat="server" Text="Slow postback - times out after one second" OnClick="slow_postback" /> <br /> 
                Last updated: <%= DateTime.Now %> 
            </ContentTemplate> 
        </asp:UpdatePanel> 
    </form> 
</body> 
 
<script type="text/javascript"> 
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function (sender, args) { 
        if (args.get_error() && args.get_error().name === 'Sys.WebForms.PageRequestManagerTimeoutException') { 
            alert('Caught a timeout!'); 
            // remember to set errorHandled = true to keep from getting a popup from the AJAX library itself 
            args.set_errorHandled(true); 
        } 
    }); 
</script> 
</html>

That's it! Maybe not immediately obvious, but also not that difficult once you've seen it done. Also note that I'm explicitly setting a one-second timeout for the purposes of this sample. You probably want a longer timeout. (The default is ninety seconds.)

Rama Krishna said:
Alternative way, The EndRequestEventArgs has a property called response which contains the WebRequestExecutor. So the above code can be written as:

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function (sender, args) {
if (args.get_error() && args.get_response().get_timedOut()) {
alert('Caught a timeout!');
// remember to set errorHandled = true to keep from getting a popup from the AJAX library itself
args.set_errorHandled(true);
}
});



Steve Marx said:
Cool, thanks Rama!
usha said:
usha said:
Thanks Rama.. This solved the problem with the Script Manager exception
Gopal said:
thanks Rama. this is exactly what i am looking for....
said:
Adrienne said:
Thank you! This is great and easy to implement.
Tanveer Asim said:
Nice Work .... i was facing this problem many days b4 and did't get any soloution ...This code sloved PageRequestManager Timeout Exception Error...Many Thanx
Amir Reza Jaberi said:
Thanks Dear Rama. this is Good sample.
said:
said:
said:
said:
said:
said:
said:
said:
said:
said:
said:
said:
said:
said:
said:
said:
said:
said:
said:
said:
said:
said:
said:
Atlanta Homes For Sale said:
Very good information here. i wish most people had the same thoughts in this business.
sell wow gold said:
http://www.mmofly.com/
http://www.mmofly.com/sell-wow-gold-1.html
http://www.mmofly.com/sell-woweu-gold-2.html
http://www.mmofly.com/sell-ffxi-gil-7.html
http://www.mmofly.com/sell-guildwars-gold-5.html
http://www.mmofly.com/sell-maplestory-mesos-6.html
http://www.mmofly.com/sell-lotro-gold-10.html
http://www.mmofly.com/sell-EverQuest_2-16.html
http://www.mmofly.com/sell-EVE_Online-14.html
http://www.mmofly.com/sell-Lineage_2-15.html
http://www.mmofly.com/sell-anarchyonline-Credits-12.html
http://www.mmofly.com/sell-Guild_Wars-5.html
http://www.mmofly.com/sell-Maple_Story-6.html
http://www.mmofly.com/sell-Final_Fantasy_XI-7.html
http://www.mmofly.com/sell-Anarchy_Online-12.html
http://www.mmofly.com/sell-World_Of_Warcraft_(_US_)-1.html
http://www.mmofly.com/sell-World_of_Warcraft_(_EU_)-2.html
http://www.mmofly.com/sell-Lord_Of_The_Ring_Online_(US)-10.html
http://www.mmofly.com/sell-Lord_Of_The_Ring_Online_(EU)-11.html
http://www.mmofly.com/sell.html
http://www.mmofly.com/trading.html
http://www.mmofly.com/cdkey.html
http://www.mmofly.com/news.html
http://www.mmofly.com/video.html
http://www.mmofly.com/about.html
http://www.mmofly.com/contact.html
http://www.mmofly.com/reg.html
http://www.mmofly.com/account.html
http://www.mmofly.com/faq.html
maple story mesos said:
http://www.cheap-msmesos.com/
http://www.cheap-msmesos.com/index.html
http://www.cheap-msmesos.com/buy_maplestory_mesos.html
http://www.cheap-msmesos.com/why.html
http://www.cheap-msmesos.com/contact.html
http://www.cheap-msmesos.com/about.html
http://www.cheap-msmesos.com/maple_story_mesos.html
http://www.cheap-msmesos.com/maple_story.html
http://www.cheap-msmesos.com/maple_story_money_cheats.html
http://www.cheap-msmesos.com/maple_story_gold.html
http://www.cheap-msmesos.com/buy_maplestory_mesos.html
http://www.cheap-msmesos.com/maple_story_hacks.html
http://www.cheap-msmesos.com/maple_story_cheats.html
http://www.cheap-msmesos.com/maple_story_items.html
http://www.cheap-msmesos.com/maple_story_tips.html
http://www.cheap-msmesos.com/maple_story_europe.html
http://www.cheap-msmesos.com/maple_story_guild.html
http://www.cheap-msmesos.com/maple_story_guides.html
http://www.cheap-msmesos.com/maple_story_archer_guide.html
http://www.cheap-msmesos.com/maple_story_private_server.html
http://www.cheap-msmesos.com/maple_story_download.html
http://www.cheap-msmesos.com/maple_story_secrets.html
http://www.cheap-msmesos.com/maple_story_map.html

http://www.allgametrade.com/
http://www.allgametrade.com/index.html
http://www.allgametrade.com/sell_to_us.html
http://www.allgametrade.com/cd-key.html
http://www.allgametrade.com/sell_wow_gold.html
http://www.allgametrade.com/aboutus.html
http://www.allgametrade.com/sell_wow_gold_us.html
http://www.allgametrade.com/sell_wow_gold_eu.html
http://www.allgametrade.com/FAQ.html
http://www.allgametrade.com/sitemap.xml
http://www.allgametrade.com/sell_maple_story_mesos.html
http://www.allgametrade.com/sell_guild_wars_gold.html
Albert said:
Albert
speed dating said:
Very good information here. i wish most people had the same thoughts in this business.
Overseas Removal said:
Great blog, great post, my blog are dofollow too.
Little Neil said:
Good Shit!
Manuel said:
Gracias por el codigo.

Saludos desde México D.F.
Javi said:
thanks a lot!!!
Czech Republic said:
Nice Site!
http://excellent-credit-card.blogspot.com
psp game downloads said:
Informative post!
Grunge said:
Thanks a lot!!!
psp games said:
Rama. Good stuff mate. Really helped.
psp game downloads said:
Wish everything was set out like this. It's so easy to understand.

Add your own comment

Your name:
Your website/mailto:
Your comment: