ViewState:为什么? [英] ViewState: why?

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

问题描述

当您在源代码中定义UserControl时,我看到的样本通常像

那样:

============

public string Text

{

get

{

String s =(String)ViewState [" Text"];

return((s == null)?String.Empty:s);

}

set

{

ViewState [" Text"] = value;

}

}

============

然而我发现这种更简单的方法效果很好

=========== =

字符串文字;

公共字符串文字

{

get {return text; }

set {text = value; }

}

============

它也不会使网页混乱(如视图状态嵌入在

页面中作为HIDDEN类型的输入。)


现在我想知道,为什么要使用ViewState?

我想有理由这样做,比如我想以编程方式更改

带有事件处理程序的控件属性并继续回发。

然而,在我看来这是一个非常简单的例子。


有更好的理由吗?

以及如何以赢得胜利的方式标记控件不要在网页中存储其视图状态




例如:我使用ViewState存储属性(如果需要存储) ,

但我不会将此视图状态输出到网页,以防它不需要

来恢复(将等于aspx)?

然而这似乎相当边缘

When you define UserControl in source code the sample I see are often like
that:
============
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["Text"] = value;
}
}
============
Yet I found that this simpler approach work well
============
string text;
public string Text
{
get { return text; }
set { text = value; }
}
============
It also doesn''t clutter the web page (as the view state is embeded in the
page as an input of HIDDEN type).

So now I wonder, why use the ViewState exactly?
I guess there are reason to do so, like if I want to programatically change
the control property with an event handler and keep on further post back.
Yet that looks to me as a very marginal exemple.

Are there better reason?
And how to mark a control in such a way that it won''t store its view state
in the web page.

Such as: I use ViewState to store property (in case they need to be stored),
yet I doesn''t output this viewstate to the web page, in case it doesn''t need
to be restored (would be equal to value in the aspx)?
Yet that seems quite a marginal

推荐答案

所有ASP。 NET控件有一个名为EnableViewState的属性,如果你不需要视图状态,你可以使用

禁用它。


Remy Blaettler

All ASP.NET Controls have a property called EnableViewState, you can
disable that if you don''t need the viewstate.

Remy Blaettler


更简单的方法不会在回发之间保留值。所以你设置

属性,用户点击一个按钮,还有另一个请求到

服务器。此时,此属性不再具有您设置的值。这是因为HTTP是无状态的,每次向服务器发出请求意味着从头开始创建一个新的
页面对象。


ViewState方法有效,因为viewstate会回发到

服务器,因此可以检索保存在其中的值。


Lloyd Dupont ; < net.galador@ld>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...
The simpler approach will not retain values between postbacks. So you set
the property, the user clicks a button, and there is another request to the
server. At this point, this property no longer has the value you set. This
is because HTTP is stateless, and each request to the server means a new
page object is created from scratch.

The ViewState approach works, because the viewstate is posted back to the
server, so the value saved in it can be retrieved.

"Lloyd Dupont" <net.galador@ld> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
当你在源代码中定义UserControl我看到的样本通常都是这样的:
============
公共字符串文本
{
得到
{String / = String} ViewState [" Text"];
return((s == null)?String.Empty:s);
}
设置
{ViewState [" Text"] = value;
}
}
============ <但是我发现这种更简单的方法效果很好
============
字符串文本;
公共字符串文本
{
get {return text; }
设置{text = value; }
}
============
它也不会使网页混乱(因为视图状态嵌入在页面中)作为HIDDEN类型的输入)。

所以现在我想知道,为什么要使用ViewState呢?
我想有理由这样做,比如我想以编程方式
用事件处理程序更改控件属性并继续发回
然后我认为这是一个非常简单的例子。

有更好的理由吗?以及如何以这样的方式标记控件,使其不会在网页中存储其视图状态。

如:我使用ViewState存储属性(以防万一)它们需要存储),但我不会将此视图状态输出到网页,以防它不需要恢复(将等于aspx中的值) ?
然而,这似乎相当边缘
When you define UserControl in source code the sample I see are often like
that:
============
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["Text"] = value;
}
}
============
Yet I found that this simpler approach work well
============
string text;
public string Text
{
get { return text; }
set { text = value; }
}
============
It also doesn''t clutter the web page (as the view state is embeded in the
page as an input of HIDDEN type).

So now I wonder, why use the ViewState exactly?
I guess there are reason to do so, like if I want to programatically
change the control property with an event handler and keep on further post
back.
Yet that looks to me as a very marginal exemple.

Are there better reason?
And how to mark a control in such a way that it won''t store its view state
in the web page.

Such as: I use ViewState to store property (in case they need to be
stored), yet I doesn''t output this viewstate to the web page, in case it
doesn''t need to be restored (would be equal to value in the aspx)?
Yet that seems quite a marginal



VieState用于在每个请求中存储每个用户信息和

postbac k。

如果您不将值存储在视图状态中,则属性值将为

丢失(因为每个请求都会生成一个新的webform对象),或者您将通过您的代码或aspx页面html内容再次设置它。


还有其他商店你可以使用(在.NEt 1.1中)

- 应用程序:所有用户都存储相同的应用程序变量。但是要小心......如果你使用网络农场,每个服务器都有自己的应用程序

集合

- 会话:每个用户都有它的拥有。如果使用Web场,则可以将其中一个服务器配置为充当状态服务器,或者可以使用SQL Server。如果你没有
,请求可能会打到农场中的另一台服务器,你将失去

会话。

- 上下文:每个请求都包含一个最终丢失的上下文集合

的请求。如果您想在所有

类中共享信息并且经常在global.asax中提交


我认为应该尽快使用viewstate你必须坚持的控件中有一个值

。将它存储在客户端具有

的优势,即服务器上使用的内存更少(想象100'的变量在

会话*用户数量---它可以非常快速地增长。

好​​的,页面上需要一些KB,但页面中最重要的是

其他资源如图片。


最后,不要忘记使用控件的开发人员可以使用

EnableViewState来禁用在客户端存储数据的功能。 />

希望有所帮助。


Steve


" Lloyd Dupont" < net.galador@ld> écritdansle message de news:

%2 **************** @ TK2MSFTNGP10.phx.gbl ...
VieState is used to store per user information among each requests and
postback.
If you don''t store the value in the viewstate, the property value will be
lost (since each request instanciate a new webform object), or you will have
to set it up again through your code or aspx pages html content.

There''s other "store" you can use (in .NEt 1.1)
- Application : All users store the same Application variables. But
beware... if you use web farm, each serveur will have its own application
collection
- Session : Each user has its own. If you use web farm, you can configure
one of the servers to act as a state server, or you can use SQL Server. If
you don''t, requests may hit another server in the farm and you''ll loose the
session.
- Context : each request hold a context collection that is lost at the end
of the request. Usefull if you want to share informations accross all your
class and often filed in global.asax

My opinion is that the viewstate should be use as soon as you have a value
in a control that must persist. Storing it on the client side has the
advantage of less memory used on the server (Imagine 100''s of variable in
the session * number of users --- it can grow very quickly).
Ok, it''s take some KB on the page, but the most weigth in a page comes from
other resources like pictures.

Finally, do not forget that a developper that use a control can use the
EnableViewState to disable the ability to store data on the client side.

Hope that help.

Steve

"Lloyd Dupont" <net.galador@ld> a écrit dans le message de news:
%2****************@TK2MSFTNGP10.phx.gbl...
当您在源代码中定义UserControl时,我看到的示例通常类似于:
============
公共字符串文本
{
get
{String / = String> ViewState [" Text"];
return((s == null)?String.Empty:s);
}
设置
{ViewState [" Text"] = value;
}
}
========== ==
然而我发现这种更简单的方法效果很好
============
字符串文本;
公共字符串文本
{
获取{return text; }
设置{text = value; }
}
============
它也不会使网页混乱(因为视图状态嵌入在页面中)作为HIDDEN类型的输入)。

所以现在我想知道,为什么要使用ViewState呢?
我想有理由这样做,比如我想以编程方式
用事件处理程序更改控件属性并继续发回
然后我认为这是一个非常简单的例子。

有更好的理由吗?以及如何以这样的方式标记控件,使其不会在网页中存储其视图状态。

如:我使用ViewState存储属性(以防万一)它们需要存储),但我不会将此视图状态输出到网页,以防它不需要恢复(将等于aspx中的值) ?
然而,这似乎相当边缘
When you define UserControl in source code the sample I see are often like
that:
============
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["Text"] = value;
}
}
============
Yet I found that this simpler approach work well
============
string text;
public string Text
{
get { return text; }
set { text = value; }
}
============
It also doesn''t clutter the web page (as the view state is embeded in the
page as an input of HIDDEN type).

So now I wonder, why use the ViewState exactly?
I guess there are reason to do so, like if I want to programatically
change the control property with an event handler and keep on further post
back.
Yet that looks to me as a very marginal exemple.

Are there better reason?
And how to mark a control in such a way that it won''t store its view state
in the web page.

Such as: I use ViewState to store property (in case they need to be
stored), yet I doesn''t output this viewstate to the web page, in case it
doesn''t need to be restored (would be equal to value in the aspx)?
Yet that seems quite a marginal



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

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