在ASP.net中禁用ViewState [英] Disabling ViewState in ASP.net

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

问题描述

我想了解更多有关ViewState的信息。我知道ViewState用于在往返中保持用户输入的数据和控制值。

我有两个问题:

1.如果我为完整页面禁用了viewstate,那么会发生什么?那我怎么能坚持数据呢?例如,如果我有一个注册页面,它有十个字段,我禁用页面的viewState,然后我如何持久保存数据?

2. MVC使用ModelBinder作为ViewState的替代或其他一些技术请告诉

I want to know more about ViewState. I know that ViewState is used to Persist User entered data and control values in round trips.
I have two Questions:
1. If I disable viewstate for complete page, then what will happened? How can then I persist the data? for eg if I have a signup page and it has ten fields, I disable viewState of the page, then How can I persist data?
2. Do MVC use ModelBinder as alternate of ViewState or some other technique Please tell

推荐答案

我将尝试回答你问题的第一部分:

1.如果我禁用viewstate完整页面,那么会发生什么?那我怎么能坚持数据呢?例如,如果我有一个注册页面,它有十个字段,我禁用页面的viewState,然后我如何保持数据?



让我们从一个非常简单的开始例。创建一个网页并添加一个文本框和一个按钮来发布该页面。禁用页面的ViewState,然后以调试模式运行页面。在texbox中添加一些文本,然后单击按钮发布页面。在页面加载中,检查文本框的值。即使ViewState被禁用,您可能会惊讶地发现您输入的值是可用的。

因此,了解ViewState存储的内容非常重要。通常,即使禁用ViewState,表单数据也可在页面中使用。 ViewState在确定更改的数据时很有用。在我们的示例中我们需要ViewState的另一个案例是,如果您要设置文本框的ForeColor属性(以及许多可用于ASP.NET TextBox控件的属性)。

通常建议禁用ViewState如果你没有使用它,因为它会使页面变得更重。

MSDN在这里有一篇关于ViewState的非常全面的文章:了解ASP.NET视图状态 [ ^ ]。

通过它,你的大多数疑惑都将被清除。

您还要问我们如何保留数据。 ViewState是asp.net中的客户端状态管理技术。还有其他客户端存储数据的机制(如隐藏变量,cookie,查询字符串等)。还有服务器端技术。同样,在MSDN上有一篇非常好的文章,它将帮助您很好地理解状态管理 - ASP.NET状态管理建议 [ ^ ]



由于我是MVC的新手,我不会回答第二部分同样。



希望有所帮助!
I will try to answer the first part of your question:
1. If I disable viewstate for complete page, then what will happened? How can then I persist the data? for eg if I have a signup page and it has ten fields, I disable viewState of the page, then How can I persist data?

Lets start by a very simple example. Create a web page and add a textbox and a button to post the page. Disable the ViewState for the page and then run the page in debug mode. Add some text in the texbox and click the button to post the page. In the page load, check the value of the textbox. You may be surprised to see that the value you entered is available even though the ViewState was disabled.
So it's important to understand what ViewState stores. Usually the form data is available in the page even with ViewState disabled. ViewState is useful in determining the changed data. Another case in our example where we would have needed ViewState is if you would have set ForeColor property (and many such which are available with ASP.NET TextBox control) of the textbox.
It's usually recommended to disable the ViewState if you are not using it as it makes the page heavier.
MSDN has a very comprehensive article on ViewState here: Understanding ASP.NET View State[^].
Go through it and most of your doubts will be cleared.
You also ask how do we persist data otherwise. ViewState is a client side state management technique in asp.net. There are other client side mechanism of storing data (like hidden variable, cookies, querystring etc). There are server side techniques as well. Again, there is a very nice article on MSDN for the same which will help you understand state management very well - ASP.NET State Management Recommendations[^]

I won't be answer the 2nd part as I am new to MVC as well.

Hope that helps!


您仍然可以继续将数据存储在会话中 - 因此禁用viewstate不会意味着您无法跨会话存储数据。
You can still go ahead and store data in sessions - so disabling viewstate does not mean you cannot store data across sessions.


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

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