Save Time With Fewer Round Trips

Avoid the double inefficiency of posting back and redirecting.

Hot Tip

LANGUAGE: C#

ASP.NET VERSIONS: 1.0 | 1.1

 

Save Time With Fewer Round Trips

Avoid the double inefficiency of posting back and redirecting.

 

By Jeff Prosise

 

Quick: Can you spot the problem in the following Web page (a button that redirects to Microsoft's home page at http://www.microsoft.com)?

 

<html>

  <body>

    <form runat="server">

      <asp:Button Text="Microsoft Home"

        OnClick="OnGotoMicrosoftHome" RunAt="server" />

    </form>

  </body>

</html>

 

<script language="C#" runat="server">

void OnGotoMicrosoftHome (Object sender, EventArgs e)

{

    Response.Redirect ("http://www.microsoft.com");

}

</script>

 

The page is simple, but it suffers a gross inefficiency that's relatively common in ASP.NET Web pages. Can you spot it?

 

If you guessed that the inefficiency stems from the number of round trips required to redirect to http://www.microsoft.com, you're correct. Clicking the button results in not one, but two, round trips over the Internet. First, the button posts back to the ASP.NET Web server that hosts the page (round trip number 1). Then that Web server uses Response.Redirect to redirect to Microsoft's home page, resulting in a 302 response that instructs the browser to go to http://www.microsoft.com (round trip number 2).

 

One of the fundamental rules for building efficient Web applications is to never do in two round trips what you could do in one. Here's a modified version of the page that's functionally identical to the previous version. But this version performs more nimbly because it gets to http://www.microsoft.com without first posting back to the server that hosts the page:

 

<html>

  <body>

    <form runat="server">

      <asp:Button ID="MyButton" Text="Microsoft Home"

         RunAt="server" />

    </form>

  </body>

</html>

 

<script language="C#" runat="server">

void Page_Load (Object sender, EventArgs e)

{

    MyButton.Attributes.Add ("onclick",

      "javascript:document.location.replace " +

      "('http://www.microsoft.com'); return false;");

}

</script>

 

In the modified page, Page_Load attaches to the button a client-side onclick attribute containing a snippet of JavaScript. Clicking the button in a DHTML-compliant browser executes the JavaScript and programmatically fetches the contents of http://www.microsoft.com. The benefits are twofold: First, the user enjoys a better experience; second, the load on your Web server is reduced.

 

Note that you can combine the two techniques - that is, attach both client-side and server-side onclick attributes to the button - to ensure that the page works equally well, regardless of whether the browser supports JavaScript. In a browser that supports JavaScript, the button will go to http://www.microsoft.com with just one round trip. In a browser that doesn't support JavaScript, the onclick attribute will be ignored and the page will post back to the server, which will in turn redirect to http://www.microsoft.com. The extra round trip will diminish performance, but it won't diminish the page's functionality.

 

Jeff Prosise is the author of several books, including Programming Microsoft .NET (Microsoft Press). He also is a co-founder of Wintellect (http://www.wintellect.com), a software consulting and education firm that specializes in .NET. Contact Jeff at [email protected].

 

 

 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish