Why is no viewState send to the Browser in this piece of aspx code

T

Tony Johansson

Hello!

Below I have four listing named (1),(2),(3) and (4)
The first one (1) is the listing for Default.aspx page that have
runat="server" on the form tag and made it
a HtmlForm control..
The second one (2) is the listing of the html code for the above
(1)Default.aspx that is send to the broswer on the client side.
The third (3) is the listings for the Default.aspx that have runat="server"
attribute removed.
The forth(4) is the listing of the html code for the above (3)Default.aspx
that is send to the brower on the client side.

Now to my question.
Why is no VIEWSTATE send to the browser when we have the runat="server"
attribute removed ?
You can see it for yourself in my listings below.



Default.aspx when we have runat="server" on the form tag
*******************************************
(1)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Testing
</div>
</form>

</body>
</html>

(2)
This html code correspond to the above Default.aspx page.
This html code is sent to the browser on the client side
*******************************************
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
Untitled Page
</title></head>
<body>
<form name="form1" method="post" action="Default.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
value="/wEPDwUJNzgzNDMwNTMzZGSK/6SoraIMYhAyosKtnFrMdvLWXQ==" />
</div>

<div>
Testing
</div>
</form>

</body>
</html>

(3)
Default.aspx when we don't have runat="server" on the form tag
************************************************
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1">
<div>
Testing
</div>
</form>

</body>
</html>

(4)
This html code correspond to the above Default.aspx page.
This html code is sent to the browser on the client side
********************************************
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
Untitled Page
</title></head>
<body>
<form id="form1">
<div>
Testing
</div>
</form>

</body>
</html>


//Tony
 
J

Jeff Johnson

Now to my question.
Why is no VIEWSTATE send to the browser when we have the runat="server"
attribute removed ?

Because for the most part ViewState exists ONLY for the purpose of
supporting controls that are run at the server. No server-side controls, no
need for ViewState. It would only be generated in that case if you
explicitly add items to it.
 
A

Arne Vajhøj

Below I have four listing named (1),(2),(3) and (4)
The first one (1) is the listing for Default.aspx page that have
runat="server" on the form tag and made it
a HtmlForm control..
The second one (2) is the listing of the html code for the above
(1)Default.aspx that is send to the broswer on the client side.
The third (3) is the listings for the Default.aspx that have runat="server"
attribute removed.
The forth(4) is the listing of the html code for the above (3)Default.aspx
that is send to the brower on the client side.

Now to my question.
Why is no VIEWSTATE send to the browser when we have the runat="server"
attribute removed ?

Without runat="server" logically it is not really a web form but just
a plain HTML file served by ASP.NET (it still get compiled etc.
like any other ASP.NET page).

No state that need to be saved and restored.

So it was optimized away. Looks smart to me.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top