ViewState问题 [英] ViewState Questions

查看:54
本文介绍了ViewState问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在学习ASP.NET并且几乎没有问题 -


1)会话ID和控件值是存储在VIEWSTATE中

变量。所以现在我们把EnableViewState =" false"在Page指令和

中禁用Web中的会话状态.Config VIEWSTATE变量仍然是

维护并存储一些值。任何人都可以告诉这些值是什么
,即除了会话ID

以及控制值以外还有什么其他信息存储在VIEWSTATE中?

2)即使我们将某些服务器控件的EnableViewState设置为false,

(例如ASP:TextBox)仍然在服务器回合后维护它们的值

旅行 - 为什么?他们不应该像HTML控件一样 - 在去服务器之后失去他们的价值

吗?


3)现在viewstate包含控制值但是什么是需要

这个?因为控件值是使用

表单的POST方法发送的。这不是两次在页面上存储相同的信息吗?可能是这是DataGrid控件所需的
,这些控件实现为Table标签,而这些

表标签不是控件,因此它们不会以POST形式发送。 (并且

因此需要存储在VIEWSTATE中)。但仍然为什么存储所有

控件的值(包括那些与表单一起发布的控件)?


4)会话ID是否可以存储在饼干而不是VIEWSTATE




可能我在这里缺少一些基本的东西,PLZ建议。

谢谢。

Pravin。

解决方案



" neo" < wo*@yahoo.com>在消息中写道

news:%2 **************** @ tk2msftngp13.phx.gbl ...



我正在学习ASP.NET并且几乎没有问题 -

1)会话ID和控件值存储在VIEWSTATE
变量中。所以现在我们把EnableViewState =" false"在Page指令和
中禁用Web中的会话状态.Config仍然维护VIEWSTATE变量并存储一些值。任何人都可以告诉我们这些值是什么,即除了会话ID和控制值之外还有什么其他信息存储在VIEWSTATE中?

2)即使我们设置了EnableViewState之后对于某些服务器控件为false,(例如ASP:TextBox)仍然在服务器
往返之后维护它们的值 - 为什么?他们不应该像HTML控件一样 - 在去服务器之后失去他们的价值吗?


ViewState不负责在回发后保留其值的文本框(以及许多其他标准的

控件)。一个文本框,复选框,

单选按钮,密码框,文本区域或隐藏表单字段不需要.NET

帮助将数据提交到服务器(HTML Post照顾

那个)。当这些值到达服务器并且服务器正准备将其回发交付回客户端时,服务器只需

获取已发布的值并进行值变为默认值

行程向下控制(即< INPUT TYPE =" Text" VALUE =" value贴在上次渲染期间
>> ;)。


因此,对于许多类似HTML的服务器控件,viewstate不是什么'

负责他们在回发中保持状态。


viewstate进来的地方就是下拉列表框。查看状态

不需要记住从列表中选择的内容,它已经习惯了

首先记住整个列表。

3)现在,viewstate包含控件值但是这需要什么呢?因为控件值是使用
表单的POST方法发送的。这不是两次在页面上存储相同的信息吗?


如上所述,那些不需要viewstate的控件因为他们使用POST而没有将他们的值存储在viewstate中开始。

如果你打开页面跟踪并查看控制树部分,你会看到这个。

可能这是
DataGrid控件需要实现为Table标签,这些
表标签不是控件,因此它们不会以POST形式发送。 (并且
因此需要存储在VIEWSTATE中)。但仍然为什么存储所有
控件的值(包括那些与表单一起发布的控件)?

4)会话ID可以存储在cookie而不是$ b $中b VIEWSTATE?

可能是我在这里缺少基本的东西,PLZ建议。
谢谢。
Pravin。



除了Scott帖子,


如果你真的想知道存储在ViewState数据中的内容,即使你已经获得了
将enableviewstate的空白页设置为false你必须调查一下

a。

1)覆盖SavePageStateToPersistenceMedium方法并检查

运行时的值三个对象的第一个参数传递为

参数。


2)关注我博客中的帖子?在ASPX文件的幕后?并且

打开asp.net为您的ASPX文件生成的类。滚动到

GetTypeHashCode函数并查看返回值。


是的,这是Viewstate页面保存的值。

GetTypeHashCode返回页面对象'

控件层次结构唯一的哈希码。

Natty Gur [MVP]


博客: http://weblogs.asp.net/ngur

手机:+ 972-(0)58-888377

***通过Developersdex发送 http://www.developersdex.com ***

不要只是参加USENET ......获得奖励!


评论内联:


" neo" < wo*@yahoo.com>在消息中写道

新闻:#M ************** @ tk2msftngp13.phx.gbl ...

1)会话控件的ID和值存储在VIEWSTATE
变量中。所以现在我们把EnableViewState =" false"在Page指令和
中禁用Web中的会话状态.Config仍然维护VIEWSTATE变量并存储一些值。任何人都可以告诉那些值是什么,即除了会话ID和控制值之外,VIEWSTATE中还存储了哪些其他信息?


首先,会话ID不存储在ViewState中。它存储在cookie中或URL QueryString中的
。在ViewState和Session State之间区分

是非常重要的。会话变量存储在服务器上的内存中(或在数据库中)。 ViewState以隐藏的形式存储值
页面上的
字段。会话超时。由于ViewState变量位于Web文档的HTML

中,因此它们不会过期。默认情况下,控制值(

某些控件)是ViewState中存储的唯一内容。但是,您也可以通过编程方式将变量添加到任何Page的ViewState中。

2)即使我们将某些服务器控件的EnableViewState设置为false,
(例如ASP:TextBox)仍然在服务器
往返之后维护它们的值 - 为什么?他们不应该像HTML控件一样 - 在去服务器之后失去他们的价值吗?


任何禁用ViewState的控件都不能保持其状态为
状态。我不知道您的应用程序有什么问题。

3)现在,viewstate包含控件值但是这需要什么呢?因为控件值是使用
表单的POST方法发送的。这不是两次在页面上存储相同的信息吗?可能这是DataGrid控件所需要的,这些控件是作为表标签实现的,并且这些表标签不是控件,因此它们不会以POST形式发送。 (并且
因此需要存储在VIEWSTATE中)。但仍然为什么存储所有
控件的值(包括那些与表单一起发布的)?


这有多种原因。 ViewState有一个特定的目的,

,它不用于存储Control的CURRENT客户端值。用于存储Control在PostBack之前具有的LAST值是

。也可以在服务器端使用

来查看自上次PostBack以来控件中的值是否已经更改

,这当然可以派上用场。等等。

4)会话ID可以存储在cookie而不是



VIEWSTATE


由于会话ID尚未存储在ViewState中,但默认情况下是存储在Cookie中的

,这个问题没有实际意义。


-

HTH,

Kevin Spencer

..Net开发人员

Microsoft MVP

大事组成了很多小东西的


hi,

I am studying ASP.NET and have few questions -

1) The session ID and values of controls is stored in VIEWSTATE
variable. So now when we put EnableViewState="false" in Page directive and
disable the session state in Web.Config the VIEWSTATE variable is still
maintained and stores some values. Can anyone tell what those values are
for, i.e what other info is stored in VIEWSTATE other than the session ID
and the control values ?

2) Even after we set EnableViewState to false for some server controls,
(e.g. ASP:TextBox) still their values are maintained after the server round
trips - Why ? Shouldn''t they act like HTML controls - loosing their values
after trip to server ?

3) Now the viewstate contains the control values but what is the need for
this ? Because the controls values are sent with the POST method of the
form. Isn''t this storing the same info twice on the page ? May be this is
needed for DataGrid controls which are implemented as Table tags and these
table tags are not controls so they are not sent with the form POST. ( and
so need to be stored in VIEWSTATE ). But still why store values of all the
controls (including those which are posted with the form ) ?

4) Can session ID be made to be stored in cookies rather than the VIEWSTATE
?

May be I am missing something basic here, plz advice.
Thanks.
Pravin.


解决方案


"neo" <wo*@yahoo.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

hi,

I am studying ASP.NET and have few questions -

1) The session ID and values of controls is stored in VIEWSTATE
variable. So now when we put EnableViewState="false" in Page directive and
disable the session state in Web.Config the VIEWSTATE variable is still
maintained and stores some values. Can anyone tell what those values are
for, i.e what other info is stored in VIEWSTATE other than the session ID
and the control values ?

2) Even after we set EnableViewState to false for some server controls,
(e.g. ASP:TextBox) still their values are maintained after the server round trips - Why ? Shouldn''t they act like HTML controls - loosing their values
after trip to server ?
ViewState is not responsible for textboxes (and many of the other standard
controls) holding on to their values after a postback. A textbox, checkbox,
radio button, password box, text area or hidden form field need no .NET
assistance to submit their data to the server (HTML Post takes care of
that). When those values arrive at the server and the server is getting the
page ready for its postback delivery back to the client, the sever just
takes the posted values and makes those values become the default values of
the controls for the trip down (i.e. <INPUT TYPE="Text" VALUE="value posted
during last rendering">).

So, for many of the "HTML-like" server controls, viewstate is not what''s
responsible for them maintaining their state across postbacks.

Where viewstate would come in would be for a drop down list box. Viewstate
isn''t needed to remember what from the list was chosen, it''s used to
remember the whole list in the first place.

3) Now the viewstate contains the control values but what is the need for
this ? Because the controls values are sent with the POST method of the
form. Isn''t this storing the same info twice on the page ?
As mentioned above, those controls that don''t need viewstate because they
are using POST, don''t have their values stored in viewstate to begin with.
If you turn on page tracing and look at the Control Tree section, you will
see this.
May be this is
needed for DataGrid controls which are implemented as Table tags and these
table tags are not controls so they are not sent with the form POST. ( and
so need to be stored in VIEWSTATE ). But still why store values of all the
controls (including those which are posted with the form ) ?

4) Can session ID be made to be stored in cookies rather than the VIEWSTATE ?

May be I am missing something basic here, plz advice.
Thanks.
Pravin.



In addition to Scott post,

If you really want to know what stored in the ViewState data even if you
got empty page with enableviewstate set to false you have to investigate
a little bit.
1) Override SavePageStateToPersistenceMedium method and inspect in
runtime the value of the First parameter of triplex object that pass as
parameter.

2) Follow the post in my blog on ?Behind the scenes of ASPX files? and
open the class that asp.net generate for your ASPX file. Scroll to
GetTypeHashCode function and check out the return value.

Yes, this is the value that saved by the page in Viewstate.
GetTypeHashCode return hash code that is unique to the Page object''s
control hierarchy.
Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377
*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!


Comments inline:

"neo" <wo*@yahoo.com> wrote in message
news:#M**************@tk2msftngp13.phx.gbl...

1) The session ID and values of controls is stored in VIEWSTATE
variable. So now when we put EnableViewState="false" in Page directive and
disable the session state in Web.Config the VIEWSTATE variable is still
maintained and stores some values. Can anyone tell what those values are
for, i.e what other info is stored in VIEWSTATE other than the session ID
and the control values ?
First of all, the Session ID is NOT stored in ViewState. It is stored either
in a cookie, or in URL QueryString. It is very important to discriminate
between ViewState and Session State. Session variables are stored in memory
(or in a database) on the server. ViewState stores values in a hidden form
field on the Page. Sessions time out. As ViewState variables are in the HTML
of a web document, they do NOT expire. By default, Control values (of
certain controls) are the only thing stored in ViewState. However, you can
also programmatically add variables to any Page''s ViewState.
2) Even after we set EnableViewState to false for some server controls,
(e.g. ASP:TextBox) still their values are maintained after the server round trips - Why ? Shouldn''t they act like HTML controls - loosing their values
after trip to server ?
Any Control that has ViewState disabled should not be able to maintain its
State. I don''t know what the problem is with your app.
3) Now the viewstate contains the control values but what is the need for
this ? Because the controls values are sent with the POST method of the
form. Isn''t this storing the same info twice on the page ? May be this is
needed for DataGrid controls which are implemented as Table tags and these
table tags are not controls so they are not sent with the form POST. ( and
so need to be stored in VIEWSTATE ). But still why store values of all the
controls (including those which are posted with the form ) ?
There are any number of reasons for this. ViewState has a specific purpose,
and it is NOT for storing the CURRENT client-side value of a Control. It is
for storing the LAST value the Control had prior to PostBack. It can also be
used on the server side to find out if the value in a Control has changed
since the last PostBack, which can certainly come in handy. And so on.
4) Can session ID be made to be stored in cookies rather than the


VIEWSTATE

Since Session ID is already NOT stored in ViewState, but by default is
stored in a Cookie, this question is moot.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.


这篇关于ViewState问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆