什么时候需要使用的ViewState [英] When do I need to use ViewState

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

问题描述

我很困惑于如何使用的ViewState 在C#中,例如什么是使用的好处:

 的ViewState [VST] =值1;

Lable1.Text =的ViewState [VST]的ToString()。
 

虽然我可以使用:

 字符串容器=值1;
Lable1.Text =容器;
 

解决方案

您ViewState中包含的保持与后背上你的页面的变量,因为它们被发送到客户端和客户端发送回与整个页。

因此​​,如果你做的:

 字符串容器=值1;
Lable1.Text =容器;
 

那么用户看到的页面,并点击提交按钮,你的容器字符串将不存在了。

然而,如果你使用的ViewState,的ViewState [VST]将仍然具有价值,因为它会被刷新当用户提交并发送页面返回。

了解更多这里也明白了ASP.NET页面的生命循环。

I am confused in how to use ViewState in C#, for example what is the benefit of using:

ViewState["VST"]="Value1";

Lable1.Text= ViewState["VST"].ToString();

Whereas I can use:

string container= "Value1";
Lable1.Text= container;

解决方案

Your ViewState consists of variables that are kept with the post-backs of your page, because they are sent to the client and the client sends them back with the entire page.

Hence, if you do:

string container= "Value1";
Lable1.Text= container;

Then the users sees the page and hits the submit button, your container string will not exist.

If however you uses the ViewState, ViewState["VST"] will still have the value as it will be "refreshed" when the user submits and sends the page back.

Read more here and also understand the ASP.NET page life cycle.

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

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