Channels ▼
RSS

.NET

What's New in Visual Studio 2010 & .NET 4 for Web Developers


ViewState

Another major piece of feedback that we received is around ViewState in ASP.NET Web Forms. ViewState gives you the ability to store the state of your application encoded as a string in a hidden form on your page. In earlier versions of ASP.NET, ViewState was turned on for all controls by default, whether you needed it or not, resulting in very large values in these hidden forms, affecting performance and manageability of your page. With .NET 4 we've turned this around, by turning it off by default, and allowing you to enable it for the controls that need it, providing you with the ability to optimize the size of your ViewState. You control this using the ViewStateMode property on either the page itself, or on any of the controls on the page.

For example, take a look at this Grid Control:


<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
    AllowSorting="True" AutoGenerateColumns="False" BackColor="White" 
    BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4" 
    DataKeyNames="id" DataSourceID="SqlDataSource1" ViewStateMode="Enabled">

When this is run on a page, the ViewState of the page will look something like this:


<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="Ri70Kt932u5kPOG5Zst6eq6fqmbiORZJcKv2ci2dhihsohA7cc2NUN8e0NhVdW7+XHOaL5qIsxcpYHk7HCvanrASFuMIK+7NUuYWweSA4Fi8QgSCxFKLiplx2WYu2NY5MR0Z7HnJGaj749Mduub4xO5dtOrZYF35/ujVrrWYVQl3ZNJvSpGHyqKBbsEk2Ry3bQAOPkdewt3eQ29uPq9v5T1B8FCdVFcOb6Uz6tgQsfqiqD3KG3MitqzP+x2d3f1AgOAnsEW363SyPOdrSPebvx3ng3EoxegtfKEpPlBXLL84HCqDV0yP2TNytEBZuqml5o1J7uUwP6ve9Epnf0cDZ/jIhl6fNd0zUcHZcchl1OhMiIILEkHGxCSqsE79JnohSz3HEQs4hRGbGaYXCs7OKEYeZxWeQBC/l0GCUGz2kfu5/dh4E5by7aeWbw2TjgERCTYts6q0uPlnzyYzk0TFaQ0IkY0Ni2yzhtLNix8YYng6ifmqnV6ogrLlbzzptchyOPlM0WOXLY0BkExE3AzybdjvYUsftACdoT2L8RYNYA+Xo7GEnhgJxGmMQ/Nwm1yVt7aPeFtSs3BRma4HrfmSGeEa4buyou+AaX8vre6IkU8nb3DOV+0Frf7XAnN97PsyXoaGQu/1VhymqWcVj6OzoH0Oay6fJJVzCU5rlFVvBqHkDjDnKDkxXlYgwH8w2A+G17h+SDjrI2Xkq+dckDF+NZLf2jrOGOW8M0qrs00xJRNpwOHeL5vv09qRqJQjELIrOury/uBQICcQ6iy7f3pQ+g5KArmz+n7PXf/SZlyOBPow+iRkuDa293xGt+2zOSOH5Y0QP96UwegRJraTha99mnfT25Xv3/6vjwIP74cu2+mdfOmtjILGyHxkXWParuvMyq99pr7Ts8+MhY1/er9RqccOB3rKLbBTAOWDrbZo0t7c9P0Dj50Q3G7qkuGVJc+HnBd5I104xUVQDu8KOdAnH4ZeeVUjtrjywFWcLZV0rvna9nThhBhGQreU7nCZbLZGMz38HyzkI/HdxXxTmu0nlg==" />
</div>

Now, if I disable the ViewState for the Grid (by setting the ViewStateMode attribute to 'Disabled'), the ViewState for the page is drastically reduced to this:


<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="P7TsNJO0o83Ega0yoyRXs+hV4rIR6f7kZ2PQoMesCBLPN1/eRlBBD56zVlYbz1mKx4snc2eVxCliiCUIYPld3oko/g6hILIeDJmL82wjhmP7zFpHCWhpmVw1no5zZAPGC7uVr8QUxt/c5HEtQFRyJxSkcWdXfTzXq6YheRAnmd0=" />
</div>

ViewState is useful in allowing you to manage data across page postbacks and making it easier for you to be productive in building applications -- we hope that the new feature to have fine control over your ViewState will give you a sweet spot in optimizing your applications.

Routing

When it comes to SEO, an important feature is to create SEO-friendly URLs. This is typically seen as the realm of MVC, where you can have a route, such as /Home/Accounts/Laurence instead of the location of a page with a bunch of parameters, such as /home/Accounts.aspx?id=Laurence. The good news is that this routing is available to you as an ASP.NET Web forms developer to provide these SEO-Friendly URLs.

You can define these routes in your global.asax file using a new property on your page called RouteData. Also, if you are using data controls, and want to pull the parameter in from the route, there is a new control called <asp:RouteParameter> which provides this functionality. Your Global.asax should look something a little like this:


…
using System.Web.Routing;
namespace RouteSampler
{
    public class Global : System.Web.HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            RegisterRoutes(RouteTable.Routes);
        }
        void RegisterRoutes(RouteCollection routes)
        {
            routes.MapPageRoute(
                "home",
                "home/{Accounts}/{name}",
                "~/Accounts.aspx",
                true);
        }
…

Here you can see that we are registering a route home/Accounts/parameter to map to the page called Accounts.aspx. So if the user types that at the end of the URL, then ASP.NET will locate the Accounts.aspx page and execute it. The browser will never see the aspx page address, just its output. You can derive the parameter(s) of the route from the RouteData property of the page. Here's an example:


string name = this.RouteData.Values["name"] as string;


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video