Solved

Removing _VIEWSTATE

Posted on 2007-04-04
3
930 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

863 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

23 Experts available now in Live!

Get 1:1 Help Now