Link to home
Start Free TrialLog in
Avatar of poogy21
poogy21

asked on

Removing _VIEWSTATE

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
ASKER CERTIFIED SOLUTION
Avatar of mirmansoor
mirmansoor

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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