Solved

Removing _VIEWSTATE

Posted on 2007-04-04
3
923 Views
Last Modified: 2013-11-26
Hello Experts,

I have an ASP.NET 2.0 Web Application, which is written in C# on VS.NET 2005 Arc.
Aside from a repeater control on my page, I produce all html elements manually (raw html - no web controls.).  I also don't care for controling state from / in UI.  

I'm trying to completely remove all traces of the _VIEWSTATE input.  I disabled viewstate on the page level and in the FORM element.  But I can't seem to get rid of the last <intput name="_VIEWSTATE" ..>.

I'm concerned about the value effecting Search Engine Optimization (SEO).  I have two reasons for this concern;
1. I don't want any additional content effecting key-word ratio, and..
2. I'm concerned that if the viewstate values change from user to user / hour to hour / version to version.. etc; this may effect the concistantcy and validation process of SEO.

Please.. if you could tell me how to get rid of _VIEWSTATE forever, I would be very greatful.

- Eyal
0
Comment
Question by:poogy21
3 Comments
 
LVL 5

Accepted Solution

by:
mirmansoor earned 500 total points
ID: 18849264
you could disable the ViewState for your application by adding this tag in <system.web> section of web.config
<pages enableViewState="false" />
0
 
LVL 15

Expert Comment

by:crisco96
ID: 18849604
If you have a form with runat="server" you will get a __viewstate variable in the html.  Disabling it at the page and control level will reduce the size but it will always exist as long as you have a server side form, .Net has to use 20 bytes of data in the viewstate no matter what.

Your only option for getting rid of __viewstate is if you remove the server side form.
0
 
LVL 2

Expert Comment

by:balochdude
ID: 18849740
Add the following code to it and it should move the viewstate to the bottom of the page. Google crawler leaves the page if there is too much encrypted data at the top of the page. If you want you can use this code to completely remove the viewstate from the xhtml.

What i do is that i normally add this code to my basepage.

C# CODE
protected override void Render(System.Web.UI.HtmlTextWriter writer)
    {
        StringWriter stringWriter = new StringWriter();
        HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
        base.Render(htmlWriter);
        string html = stringWriter.ToString();
        int StartPoint = html.IndexOf("<input type=\"hidden\" name=\"__VIEWSTATE\"");
        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("</form>");
            if (FormEndStart >= 0)
                html = html.Insert(FormEndStart, viewstateInput);
        }
        StartPoint = html.IndexOf("<input type=\"hidden\" name=\"__EVENTTARGET\"");
        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("</form>");
            if (FormEndStart >= 0)
              html = html.Insert(FormEndStart, viewstateInput);
       }
        StartPoint = html.IndexOf("<input type=\"hidden\" name=\"__EVENTARGUMENT\"");
        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("</form>");
            if (FormEndStart >= 0)
               html = html.Insert(FormEndStart, viewstateInput);
       }
        html = html.Replace("<script>", "<script type=\"text/javascript\">");
        html = html.Replace("language=JavaScript", "language=\"javascript\"");
        writer.Write(html);
  }


VB CODE
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)

            Try
                Dim stringWriter As StringWriter = New StringWriter
                Dim htmlWriter As HtmlTextWriter = New HtmlTextWriter(stringWriter)
                MyBase.Render(htmlWriter)
                Dim html As String = stringWriter.ToString
                Dim startPoint As Integer = html.IndexOf("<input type=""hidden"" name=""__VIEWSTATE""")
                If startPoint >= 0 Then
                    Dim endPoint As Integer = html.IndexOf("/>", startPoint) + 2
                    Dim viewStateInput As String = html.Substring(startPoint, endPoint - startPoint)
                    html = html.Remove(startPoint, endPoint - startPoint)
                    Dim formEndStart As Integer = html.IndexOf("</form>")
                    If formEndStart >= 0 Then
                        html = html.Insert(formEndStart, viewStateInput)
                    End If
                End If
                startPoint = html.IndexOf("<input type=""hidden"" name=""__EVENTTARGET""")
                If startPoint >= 0 Then
                    Dim endPoint As Integer = html.IndexOf("/>", startPoint) + 2
                    Dim viewstateInput As String = html.Substring(startPoint, endPoint - startPoint)
                    html = html.Remove(startPoint, endPoint - startPoint)
                    Dim formendstart As Integer = html.IndexOf("</form>")
                    If formendstart >= 0 Then
                        html = html.Insert(formendstart, viewstateInput)
                    End If
                End If
                startPoint = html.IndexOf("<input type=""hidden"" name=""__EVENTARGUMENT""")
                If startPoint >= 0 Then
                    Dim endPoint As Integer = html.IndexOf("/>", startPoint) + 2
                    Dim vewistateInput As String = html.Substring(startPoint, endPoint - startPoint)
                    html = html.Remove(startPoint, endPoint - startPoint)
                    Dim formEndStart As Integer = html.IndexOf("</form>")
                    If formEndStart >= 0 Then
                        html = html.Insert(formEndStart, vewistateInput)
                    End If
                End If
                html = html.Replace("<script>", "<script type=""text/javascript"">")
                html = html.Replace("language=Javascript", "language=""javascript""")
                writer.Write(html)
            Catch ex As Exception
                ErrorLog.WriteError(DBEngine.DbConn, DBEngine.CurrentUserId, ex.Message, Security.GetVisitorIPAddress(Me), True, "FinanceWeb.App_Code.Page.Render Override")
            End Try

        End Sub
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now