Reader Level:
ARTICLE

Features in ASP.NET 4.0

Posted by Ashish Shukla Articles | .NET 4.5 May 09, 2010
ASP.NET 4.0 includes enhancements in many areas. This article is an overview of the new and major improvements that are included in the ASP.NET 4.0.
  • 0
  • 0
  • 11969

ASP.NET 4.0 includes enhancements in many areas. This article is an overview of the new and major improvements that are included in the ASP.NET 4.0.

Output Caching

Since  ASP.NET 1.0 , output caching has is being used to store the generated output of pages and controls. On subsequent requests ASP.NET can serve the HTML content more quickly from memory instead of  generating the output from scratch . However this approach has the limitation that the generated output always has to be stored in memory and if the website experiences heavy traffic the memory requirements can be drastic.

ASP.NET improves output caching by providing output-cache providers. Output-cache providers can use any storage medium to store the generated output. These storage medium can vary from local or remote disks to cache engines.

A new cache provider can be created deriving from OutPutCacheProvider class . We can configure the Cache provider in the Web.config file in the new providers subsection of the outputCache element.

We can us the different provider for different controls using the providerName attribute of the Output cache element.

<%@ OutputCache providerName="MyCache" %>


Redirecting page to a new link

The website content is moved during the lifetime of the web application from one URL to other.This was handled by using the Redirect method to redirect to the new URL. One disadvantage with this method is that the Redirect method results in extra roundtrip to the server.

ASP.NET 4 introduces the RedirectPermanent method.

Search engines can store the new URL associated with the content removing the need to use the Redirect method for moved content. 

This method can be used to move to the new link:

Response.RedirectPermanent("/NewURL.aspx");


Enabling Viewstate on Control basis. 

In the previous versions of ASP.NET we can't turn off the viewstate for entire page and then unable it for some of the controls that need it. We can do so in ASP.NET 4.0.This is more convenient than disabling the viewstate on control by basis.You can set the ViewStateMode for the page to false and then enable it for individual controls.

Row Selection in GridView and ListView

In previous versions of ASP.NET in Gridview and Listview rowselection was on the basis of RowIndex. If we selected nth row in a page and moved to other page then nth row was selected on the moved page as well.

It is more desirable not to select any row in the moved page and when the user comes back on the original page he should see the nth row selected . ASP.NET 4 provides persisted selection in which row selection is based on the data key. We enable persisted selection by using the PersistedSelection attribute to "true" in the GridView Control.

Session State Compression

There are two options to store session state data out of process  one is session state provider that stores data in session state server and second is session state provider that stores data in a Microsoft SQL Server database  .The size of the serialized data stored in the session state server and SQL Server can be too large. In ASP.NET 4 there is compression option to store the serialized data. This option can be enabled using compressionEnabled attribute in Session State element.

<sessionState
mode="SqlServer"
sqlConnectionString="data source=myserver;Initial Catalog=aspnetstate"
compressionEnabled="true"
/>

ClientIDMode Property for generated ClientID

Every asp.net control is rendered as an HTML element and a corresponding ClientId is generated for it.If we want to reference  the generated HTML element in javascript we must know the Id attribute of the generated element. The ID attribute in HTML that is rendered for Web server controls is generated based on the ClientID property of the control. 

Until ASP.NET 4, we do not have much control over the generated Id of the Control. The new ClientIDMode property lets you specify more precisely how the client ID is generated for controls.

Possible settings for ClientIDMode are following:
  • AutoID - Generated ClientID  is similar to earlier versions of ASP.NET.Difficult to predict generated Id values.
  • Static - This specifies that the ClientID value will be the same as the ID. For example if our label control has id HeaderLabel then the ClientId will also have value of HeaderLabel
  • Predictable - This setting is used mostly in controls that are inside Data Bound Controls. The  ClinetId value is generated by concatenating the ClinetId value of the parent naming container with the ID value of the control. This setting  works in conjunction with the ClientIDRowSuffix property

    If specified the value of the data field specified in the ClientIDRowSuffix property is added at the end.

    For instance if we have StudentName Label inside ListView1  Student Name labels ClinetIDMode is set to Predictable and ClientIDRowSuffix is set to "ID" then StudentName labels' generated client id's will be as :

    ListView1_ StudentName _1
    ListView1_ StudentName _2
    ListView1_ StudentName _3
    ListView1_ StudentName _4
  • Inherit - The control inherits the ClientIDMode setting of its parent control. This is the default value for control
ListView Control 

Unitl ASP.NET 3.5 ListView control required ItemTemplate and LayoutTemplate. ItemTemplate specifies the markup for  each item bound to the ListView while LayoutTemplate defines just the enclosing markup for the items in the List View.

The LayoutTemplate serves no purpose but to specify where the markup of ItemTemplate should appear.

In ASP.NET 4.0 we don't need the LayoutTemplate in ListView,the ItemTemplate is the only required template.

So instead of the following markup :

<asp:ListView ID="ListView1" runat="server" ...>
   <LayoutTemplate>
      <asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
   </LayoutTemplate>
   <ItemTemplate>
      ...
   </ItemTemplate>
</asp:ListView>

We just need 

<asp:ListView ID="ListView1" runat="server" ...>
   <LayoutTemplate>
      <asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
   </LayoutTemplate>
   <ItemTemplate>
      ...
   </ItemTemplate>
</asp:ListView>

COMMENT USING

Trending up