Solved

Removing _VIEWSTATE

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

713 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