Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Removing _VIEWSTATE

Posted on 2007-04-04
3
Medium Priority
?
942 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 5

Accepted Solution

by:
mirmansoor earned 1000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

670 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