Selasa, 26 Agustus 2008

ViewState, SEO, Size

What is ViewState and what it is not?

There are some common misconceptions about ViewState. Let me discuss these points here for the benefit of the readers. ViewState does not hold the controls, rather it holds the values of the form controls and their corresponding ID's that would otherwise be lost due to a post back because they do not post with the form. ViewState is not used to hold session data or to transmit data between pages. ViewState does not recreate the dynamically created controls of a page. It does not restore the values to the controls after a post back operation. Taken aback? Yes, it is true. Even when the ViewState for a control is disabled, still the value would be retained after a post back of the page occurs, for input controls like TextBox or DropDownList. So then, what is ViewState? ViewState represents the state of a page when it was last processed on the web server. It holds the values of a control that has been dynamically changed.

How does ViewState work?

All server controls have a property called ViewState. If this is enabled, the ViewState for the control is also enabled. Where and how is ViewState stored? When the page is first created all controls are serialized to the ViewState, which is rendered as a hidden form field named __ViewState. This hidden field corresponds to the server side object known as the ViewState. ViewState for a page is stored as key-value pairs using the System.Web.UI.StateBag object. When a post back occurs, the page de-serializes the ViewState and recreates all controls. The ViewState for the controls in a page is stored as base 64 encoded strings in name - value pairs. When a page is reloaded two methods pertaining to ViewState are called, namely the LoadViewState method and SaveViewState method. The following is the content of the __ViewState hidden field as generated for a page in my system.

Enabling and Disabling ViewState

By default, ViewState is enabled for all server controls. ViewState can be enabled and disabled in any of the following ways.

* Page Level
* Control Level
* Application Level
* Machine Level

To enable or disable ViewState in the Page Level, use the following in the Page directive of the ASP.NET page.

<%@ Page EnableViewState ="False" %>
or
<%@ Page EnableViewState ="True" %>

To enable or disable ViewState at the Control Level, use the following:
asp:TextBox id="txtCode" runat="server" EnableViewState="false"
or
asp:TextBox id="txtCode" runat="server" EnableViewState="true"

To enable or disable ViewState in the Application Level, use the following:
pages enableViewState="false"
or
pages enableViewState="true"


To enable ViewState in the Machine Level, use the following:
pages enableViewState="true" enableViewStateMac="true"
or
pages enableViewState="false"


For SEO

Its important to moving ViewState to the bottom of the page, this is example

protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
System.IO.StringWriter stringWriter = new System.IO.StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
base.Render(htmlWriter);
string html = stringWriter.ToString();
int StartPoint = html.IndexOf("
if (StartPoint >= 0)
{
int EndPoint = html.IndexOf("/>", StartPoint) + 2;
string viewstateInput = html.Substring(StartPoint, EndPoint - StartPoint);
html = html.Remove(StartPoint, EndPoint - StartPoint);
int FormEndStart = html.IndexOf("") - 1;
if (FormEndStart >= 0)
{
html = html.Insert(FormEndStart, viewstateInput);
}
}
writer.Write(html);
}

Calculate Size

To Calculate Viewstate size :
protected override void SavePageStateToPersistenceMedium(object viewState)

{
base.SavePageStateToPersistenceMedium(viewState);
LosFormatter format = new LosFormatter();
StringWriter writer = new StringWriter();
format.Serialize(writer, viewState);
int viewStateLength = writer.ToString().Length;
}


To Calculate page size :

protected override void Render(HtmlTextWriter writer)
{
StringWriter customWriter = new StringWriter();
HtmlTextWriter localWriter = new HtmlTextWriter(customWriter);
base.Render(localWriter);
int pageLength = customWriter.ToString().Length;
writer.Write(customWriter.ToString());
}



If your application using friendly url and using new extension, application will be give you error. you must setting your IIS, this very easy and simple, like these :
  1. open IIS
  2. right click at the apllication which is having friendly url. select Properties.
  3. choose tab Home Directory and click button Configuration.
  4. at tab Mapping, click button Add.
  5. fill Executable by browse C:\Windows\Microsoft .Net\Framework\v2.0.50727\aspnet_isapi.dll
  6. fill extension by your new extension
  7. select All Verbs
  8. check Script Engine
  9. don't check Verify that File Exist
  10. then click OK
  11. now try your SEO

source :
http://www.dotnetjohn.com/articles.aspx?articleid=247
http://www.hanselman.com/blog/MovingViewStateToTheBottomOfThePage.aspx
http://www.codeproject.com/KB/viewstate/ViewStateCompression.aspx
http://bytes.com/forum/thread300574.html

Tidak ada komentar: