Techbubbles

Web Forms model in ASP.NET 4.0

ASP.NET 4.0 platform is built up with various components Web Forms,ASP.NET MVC, Dynamic Data Controls and ASP.NET AJAX. It has the same foundation as in ASP.NET 3.5 SP1 but refined the above features. This post speaks about features in the Web Form model.

         ASP


ASP.NET 4.0 features are nothing new, all are in 3.5 but this version gives more control over frequently used features.

Example: ASP.NET 4.0 Web Forms give developers more control over viewstate management, generation of Control ID’s, and HTML generated by some template based controls.

Control Over the Viewstate

Every developer knows that ASP.NET Viewstate burdens the page and waste of bandwidth. Same developers welcomed the ASP.NET MVC because of its complete absence of viewstate. If you ignore the viewstate on your page then you need to reload the data from server to controls.

The Viewstate is functional to Web Forms model, as it caches the contents from cache for control in the page.The overlooked feature is we can turnoff the viewstate for the page or control.The viewstate support is turned on for each page by default. The property EnableViewstate is defined is System.Web.UI.Control class and can be used to turn it on or off.

You can turn off the viewstate for ASP.NET page either declaratively or programmatically during the page’s life cycle.

void Page_Load(object sender,EventArgs e)
{
//Disable viewstate for the page and all of its child controls

this.EnableViewState = false;

....

}

Viewstate setting in ASP.NET has hierarchical nature, which means if the viewstate is enabled on the parent control, it can not be disabled on any of its child controls. You can disable the viewstate at page level and enable it in control level wherever it required.

ASP.NET 4.0 feature is you can enable viewstate at control level. In ASP.NET 4.0, the System.Web.UI.Control class exposes a new property named ViewStateMode:

public virtual ViewStateMode {get; set; }

ViewStateMode enumeration has the following values

 

Value Description
Inherit Inherits the value of ViewStateMode property from the parent control
Enabled Enables viewstate for this control even if the parent control has set the viewstate property disabled.
Disabled Disables viewstate for this control even if the parent control has set the viewstate property enabled.

 

Auto-Generated IDs

It is possible that rendered HTML can contain the same ID. When search for an element using getElementById, you will simply get an array of elements.Most data-bound controls generate their output by repeating the HTML for every data-bound item.

The sample generated Id string look like the following

ctl00$ContentPlaceHolder2$Gridview11$TextBox1

First issue might be with the length of the string, which repeated for several elements, makes the downloaded larger. Predicting the ID of a given control from script is difficult.

A frequently used technique for the above issue is

var btn = document.getElementById("<%=Button1.ClientID %> ");

ASP.NET 4.0 supports another option for autogenerated ids problem and developer can has greater control over generating the clientid of a control.

The System.Web.UI.Control Class now has a brand new property named ClientIDMode.

The ClientIDMode property values can be

Value Description
Legacy Indicates that ID should be generated as in earlier versions of ASP.NET
Static ASP.NET doesn’t make any attempt to scope the client ID. The ID is assigned as-is.
Predictable The ID is obtained by simply concatenating the ID of parent controls and ignoring master page’s parent elements.
Inherit The control will use the same algorithm as its parent.

Consider the following code:

<asp:GridView ID="GridView1" runat="server" 
              ClientIDMode = "Predictable"
              RowClientIdsuffix="CustomerID">
  ......

</asp:GridView>

In this case, each row of the grid is identified by one or more columns in the data source with a trailing index. example

Panel1_GridView1_ALFKI_1

Finally, note that the ClientIDMode property affects only the ID attribute of the resulting HTML. The more about clientids in ASP.NET 4.0 can read here

HTML Enhancements

In the early version of ASP.NET developer didn’t have much control over programmatically accessing the HTML tags of a Web Page.

In ASP.NET 4.0, the Page class exposes two new string properties to set some common tags in the <head> section of a page. The two properties are Keywords and Description. The Keywords and Description properties can also be set directly as attributes of the @Page directive as shown below

<%@ Page Language="c#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Keywords="ASP.NET, AJAX " Description="ASP.NET 4.0 WebForms" %>

 

permanently redirecting the page feature can be found here

More Control over Output Caching here

Conclusion

ASP.NET 4.0 Web Forms contains a number of features that together can make it a bigger development platform. It is moreover a refinement of existing features.

Source: MSDN Magazine

Share this post :


Related Posts:

%d bloggers like this: