Save Bandwidth When Using DataGrids

Don't slow down requests with view-state data.

Hot Tip

LANGUAGES: C#

TECHNOLOGIES: DataGrids

 

Save Bandwidth When Using DataGrids

Don't slow down requests with view-state data.

 

By Jeff Prosise

 

DataGrids are one of the most popular - if not the most popular - of all the ASP.NET server controls. One well-placed DataGrid can replace reams of old ASP code that queries a database and renders a recordset into HTML. But DataGrids have a dark side, too. They save their contents in view state so they can retain content across postbacks. A large DataGrid can massively decrease the effective bandwidth of a connection because more content means more view state. Consider the following .aspx file, which uses a DataGrid to render a modest number of records from the SQL Server Pubs database:

 

 

<%@ Import Namespace="System.Data.SqlClient" %>

 

<html>

  <body>

    <form runat="server">

      <asp:DataGrid ID="MyDataGrid" RunAt="server" />

    </form>

  </body>

</html>

 

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

  void Page_Load (Object sender, EventArgs e)

  {

    if (!IsPostBack) {

      SqlConnection connection = new SqlConnection

        ("server=localhost;database=pubs;uid=sa;pwd=");

 

      try {

        connection.Open ();

        SqlCommand command = new SqlCommand

          ("SELECT title, ytd_sales, notes FROM titles",

          connection);

        SqlDataReader reader = command.ExecuteReader ();

        MyDataGrid.DataSource = reader;

        MyDataGrid.DataBind ();

      }

      finally {

        connection.Close ();

      }

    }

  }

</script>

Figure 1. This innocuous-looking file is actually a bandwidth hog.

 

Looks simple, right? It is simple. But it also round-trips about 6 K of view state data in every request. Open this page in your browser and execute a View/Source command and you'll see for yourself. View state is the large base-64 value attached to the <input> control named __VIEWSTATE. Now imagine a DataGrid that renders thousands of records instead of only a few and you easily can see why DataGrids often are first-rate bandwidth eaters.

 

So what can you do about it? Simple: Turn view state off. Here's a modified version of the page that prevents the DataGrid from writing its contents to view state:

 

<%@ Import Namespace="System.Data.SqlClient" %>

 

<html>

  <body>

    <form runat="server">

      <asp:DataGrid ID="MyDataGrid" EnableViewState="false"

       RunAt="server" />

    </form>

  </body>

</html>

 

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

  void Page_Load (Object sender, EventArgs e)

  {

    SqlConnection connection = new SqlConnection

      ("server=localhost;database=pubs;uid=sa;pwd=");

 

    try {

      connection.Open ();

      SqlCommand command = new SqlCommand

        ("SELECT title, ytd_sales, notes FROM titles",

       connection);

      SqlDataReader reader = command.ExecuteReader ();

      MyDataGrid.DataSource = reader;

      MyDataGrid.DataBind ();

    }

    finally {

      connection.Close ();

    }

  }

</script>

Figure 2. By setting EnableViewState to "false," you can effectively increase the bandwidth of a connection.

 

Note the EnableViewState="false" attribute in the DataGrid tag turning view-state usage off. Is there a downside to disabling view state? There's one: The DataGrid must now be reinitialized on every request, including postbacks, or else its contents disappear. (Note the absence of the if !IsPostBack clause found in the first example.) If the DataGrid is large enough, the bandwidth you save by minimizing view-state usage could more than offset the extra cycles you expend on the server reinitializing the DataGrid. That's especially true if instead of performing a database query every time the page is fetched, you do the query once, cache the results in memory, and repopulate the DataGrid from the cache.

 

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

 

 

 

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