Forum Moderators: open

Message Too Old, No Replies

Viewstate

How to get rid of <input type="hidden" name="__VIEWSTATE"

         

promar

9:41 pm on Aug 25, 2005 (gmt 0)

10+ Year Member Top Contributors Of The Month



We never used .NET before. Now the time has come and our developer said that there is no way to exclude the <input type="hidden" name="__VIEWSTATE" bla-bla-bla> tag from the code.

It seats within the <head> tags, is fairly big and looks ugly.

Is there a way to get rid of it or move it to an external folder?

Thanks.

mrMister

10:42 pm on Aug 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If it's between your head tags then you've done something wrong. Please post your ASP.Net code and we'll have a look at it.

To disable viewstate put the following line at the top of your aspx pages.

<%@ Page EnableViewState="false" %>

Bear in mind that the viewstate is necessary in ASP.Net to maintain state for controls between pages, so if you're features that require state management, they will no longer work properly.

sharbel

11:01 pm on Aug 25, 2005 (gmt 0)

10+ Year Member



If you are not aware of how to minimize ViewState, you really should read the various articles on how to reduce it. Do some Googling on how to reduce it, and you will see. There are so many times that you don't even need ViewState on, yet all that dang bloat is being passed back in every page load.

One common misconception is that you *need* a server <form> tag for each .aspx page. This is largely due to Visual Studio plopping one in each page when you add an .aspx page to a project. Not all asp.net controls require the form.. so if your page doesn't need a server <form>, don't use it. When you omit the server <form> ViewState is not recorded and your pages are much lighter weight.

Don't get me wrong, ViewState is really nice and convenient! Just be aware of the need to mimimize it's size, and you will be in good shape.

tomasz

11:21 pm on Aug 25, 2005 (gmt 0)

10+ Year Member



I do not think you can get rid of it unless you will not use it as mentioned it this post, but you can move it to bottom of the page. Here is a code I use it was posted here by one of the WM user.

' This method overrides the Render() method for the page and moves the ViewState
' from its default location at the top of the page to the bottom of the page. This
' results in better search engine spidering.
'
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
Dim stringWriter As System.IO.StringWriter = New System.IO.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 'does __VIEWSTATE exist?
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>") - 1
If FormEndStart >= 0 Then
html = html.Insert(FormEndStart, ViewStateInput)
End If
End If
writer.Write(html)
End Sub

mrMister

11:50 pm on Aug 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Surely it just depends on where you put your <form runat="server"> tag. It should appear below wherever you put that. Doesn't VS.Net give you control over the position of the <form> rag?

sharbel

12:12 am on Aug 26, 2005 (gmt 0)

10+ Year Member



Sure, the form tag in vs.net can be moved in the HTML view.. its just a regular form tag with a runat="server" attribute

agerhart

12:20 am on Aug 26, 2005 (gmt 0)

promar

1:55 am on Aug 26, 2005 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thanks to you all.

Just spoke with our developer - looks like we need to keep viewstate (for managing sessions) but will definitely move it to the bottom of the code.

Your advices are very much appreciated.

mrMister

2:02 am on Aug 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You've used the term "external folder" before.

What do you mean by this? Just curious.

promar

2:29 am on Aug 26, 2005 (gmt 0)

10+ Year Member Top Contributors Of The Month



I meant removing viewstate from the code by placing it somewhere else.

Please don't laugh too loud if it sounds stupid - I know close to nothing about web development.

PareshJagatiya

6:49 am on Sep 22, 2005 (gmt 0)



well you can get rid of the large VIEWSTATE data
by serialising it either to the webserver(your application's virtual directory in some .txt file or you can keep it in the Session)
that can be done by overriding 2 page methods
viz. LoadPageStateFromPersistantMedium AND
SavePageStateToPersistantMedium.
See the help code at [postmaster.co.uk...]