简体   繁体   English

使用 Viewstate 的原因

[英]Reason for using Viewstate

After a lot of reading about viewstate I have a pretty solid understanding of how it works.在大量阅读有关 viewstate 之后,我对它的工作原理有了非常深刻的理解。 One thing I'm not convinced with yet is why can't I just ignore it and move all the data in the form itself.我还不相信的一件事是为什么我不能忽略它并移动表单本身的所有数据。

Can anyone please give me a use case where using viewstate is mandatory and without it there is no reasonable way of implementing the case?谁能给我一个使用视图状态是强制性的用例,没有它就没有合理的方法来实现这个案例?

In other words, give me a scenario where using viewstate is clearly beneficial.换句话说,给我一个使用视图状态显然是有益的场景。

Can anyone please give me a use case where using viewstate is mandatory and without it there is no reasonable way of implementing the case谁能给我一个使用视图状态是强制性的用例,没有它就没有合理的方法来实现这个案例

A bit of a druken roedo clown wording here, don't you think?这里有点druken roedo小丑的措辞,你不觉得吗? NEVER have I seen anyone suggest or hint or use the word "mandatory"我从未见过任何人建议或暗示或使用“强制”一词

In other words, give me a scenario where using viewstate is clearly beneficial.换句话说,给我一个使用视图状态显然是有益的场景。

Ok, now that question makes great sense.好的,现在这个问题很有意义。

And sure, I can think of a dozen cases, but lets take a simple one.当然,我可以想到十几个案例,但让我们举一个简单的案例。

I have system in which we are working on a project.我有一个我们正在处理一个项目的系统。 Lots of moving parts.很多活动部件。

The user will no doubt come from a previous page, and selected/searched from some type of search screen to select the project we are to work on.用户无疑会来自上一页,并从某种类型的搜索屏幕中选择/搜索到 select 我们要处理的项目。

so, I pass a class with all the information I need (not going to pass a nasty 10 or 20 variables in the URL, are we?).所以,我传递了一个包含我需要的所有信息的 class(不会在 URL 中传递讨厌的 10 或 20 个变量,是吗?)。

So, in my on-load event, we have this - we are comming from the project select page, and now landing on the project "edit" or "working" page for this one project (it could be any kind of project - building a boat, or whatever).所以,在我的加载事件中,我们有这个 - 我们来自项目 select 页面,现在登陆这个项目的项目“编辑”或“工作”页面(它可以是任何类型的项目 - 建设一条船,或其他什么)。

Ok, so in the first page load event, I PASS and take the session() based class and move it RIGHT AWAY into ViewState.好的,所以在第一个页面加载事件中,我 PASS 并采用基于 session() 的 class 并将其移到 ViewState 中。 (and I give a VERY good reason in a bit as to why we do this. Suffice to say, session() is global to one user, but ViewState is PER web page + per user. (关于我们为什么这样做,我给出了一个很好的理由。可以说,session() 对一个用户来说是全局的,但 ViewState 是每个用户的 PER web 页面 + 每个用户。

So, we now have tucked away the above cPinfo class into view state.因此,我们现在将上述 cPinfo class 隐藏到 state 中。

That class is a set of values and variables that I need for the code to operate correclty on this page. class 是一组值和变量,我需要这些值和变量来使代码在此页面上正确运行。

The class is this (actually, the real one is MUCH more complex, but it don't matter for this explain). class 是这样的(实际上,真正的要复杂得多,但这并不重要)。

So, our class is defined at the page class level, like this:因此,我们的 class 是在页面 class 级别定义的,如下所示:

Public Class ProjectUpload
    Inherits System.Web.UI.Page

    Dim cPinfo As New clsProjectInfo

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    etc. etc. etc. etc. -- the page code behind.

And the class is this: class 是这样的:

    <Serializable>
    Public Class clsProjectInfo

        Public ContactID As Integer = 0
        Public ContactGeneralID As Integer = 0
        Public PortalComp As String = ""
        Public ProjectHeaderID As Integer = 0
        Public QuoteNum As Integer = 0
        Public UploadGroup As Integer = 0
        Public txtNotes As String = ""
        Public ProofID As Integer = 0


    End Class

So, above has 8 values - and those 8 values are needed by BOATLOADS of code on this page.因此,上面有 8 个值 - 此页面上的 BOATLOADS 代码需要这 8 个值。

So, say I need to display some customer info on my page.所以,假设我需要在我的页面上显示一些客户信息。

Say like this:像这样说:

    Dim rst As DataTable
    strSQL = "select * from Web_ContactsSalesRep where ContactGeneralID = " & cPinfo.ContactGeneralID
    rst = Myrst(strSQL, GetConstr(cPinfo.PortalComp))
    If rst.Rows.Count > 0 Then
        With rst.Rows(0)
            strMyProjects += .Item("CompName") & "</font> - "
        End With
    End If

So, note in above, I used TWO variables (values) from that class.因此,请注意上面,我使用了 class 中的两个变量(值)。

ContactGeneralID, and PortalComp. ContactGeneralID 和 PortalComp。

And lets look at some more code.让我们看看更多的代码。

    ' get project info
    strSQL = "select * from Web_ProjectHeader where ID = " & cPinfo.ProjectHeaderID
    rst = Myrst(strSQL, GetConstr(cPinfo.PortalComp))

Again, I needed in code ProjectHeaderID同样,我需要在代码 ProjectHeaderID

And this:和这个:

    'create all project folders
    GetProjectFolder(MyPinfo.QuoteNum, MyPinfo.PortalComp)

So, in above, I needed the Quote number, and PortalComp - 2 varibles and values.因此,在上面,我需要报价编号和 PortalComp - 2 个变量和值。

I want to display project files, so this:我想显示项目文件,所以这个:

    Dim strSQL As String = "select * from WebUpLoad where ProjectHeaderID = " & cPinfo.ProjectHeaderID &
        " ORDER BY DateUpLoaded DESC"

Ok, so you get the idea?好的,你明白了吗?

I have a set of variables that boatloads of code requires.我有一组代码需要大量的变量。

Where do YOU suggest I store and persist these values that boatloads of code reuqires to operate?你建议我在哪里存储和保存这些需要大量代码才能运行的值?

I had to park that information SOME place, right?我必须把这些信息停在某个地方,对吧? So where do you propose I save nd persist that set of variables I need in code?那么你建议我在哪里保存和持久化我在代码中需要的那组变量呢?

I MOST certainly don't want to drop into that page 5 or 15 hideen fields, do I?我当然不想进入第 5 页或第 15 页的隐藏字段,是吗?

And if right now, say I add 5 more values/properties to that class?如果现在,假设我向该 class 添加了 5 个值/属性? Then ALL my code and ALL my pages?然后我的所有代码和我的所有页面? They now have 100% free use of those values in that class.他们现在可以 100% 免费使用 class 中的这些值。

So, next issue:那么,下一期:

Why did I not use session()?为什么我没有使用 session()?

Well, as noted, Viewstate is per page, and session() is global to the one user.好吧,如前所述,Viewstate 是每页的,而 session() 对一个用户来说是全局的。

But, I can't use session() to persist those values, since what happens if the user opens another copy of their browser, goes back to the project selection page, and now selects a different project to work on?但是,我不能使用 session() 来保存这些值,因为如果用户打开浏览器的另一个副本,返回项目选择页面,现在选择另一个项目来处理会发生什么?

Well, better example?那么,更好的例子? You on a site to buy a house.你在一个网站上买房子。 You select the house on one page, and use session() to store all kinds of information about that house (variables and values in you need in code).你 select 在一页上的房子,并使用 session() 来存储关于该房子的各种信息(你需要在代码中的变量和值)。 Now you jump to the next page to buy the house.现在你跳到下一页买房子。

if you use session(), then you can wind up with two web pages open, but session() will have all the values based on the LAST house you picked from the previous web page.如果您使用 session(),那么您最终会打开两个 web 页面,但 session() 将具有基于您从上一个 web 页面中选择的最后一个房屋的所有值。

If you click on buy house, you can't use that session() value(s) any more.如果您单击购买房屋,则不能再使用该 session() 值。

So, I follow a design pattern in which I use session() to pass the "set" of variables to the next page, but FIRST code execute is to transfer from session() to ViewState.因此,我遵循一种设计模式,其中我使用 session() 将变量“集合”传递到下一页,但执行的第一个代码是从 session() 传输到 ViewState。 From that point on, ALL CODE on that page will use ViewSate.从那时起,该页面上的所有代码都将使用 ViewSate。

This way, the fact that session is global to the user don't matter.这样,session 对用户来说是全局的这一事实并不重要。 If they launch another copy of their browsewr, select a house (or project or whatever), then I use session to pass to the next page, but from that point on, the values I passed are now in ViewState - and ViewState is per web page - not global to he user.如果他们启动另一个浏览器副本 select 房屋(或项目或其他),那么我使用 session 传递到下一页,但从那时起,我传递的值现在在 ViewState - 并且 ViewState 是每个 session页面 - 对他的用户来说不是全局的。

Now the above is only one of many examples.以上只是众多示例之一。

but, simple answer this question:但是,简单地回答这个问题:

You need a set of values and variables to persit for the current page.您需要一组值和变量来保留当前页面。 Say 5, or 8 or 15 values.说 5、8 或 15 个值。 Where are you going to put them?你要把它们放在哪里?

Surely you don't suggest that in each web page, I drop in 15 fields, do you?您肯定不会建议在每个 web 页面中,我放入 15 个字段,对吗?

Now, I could I suppose sterilize the class into json, and have ONE hidden field on the page, but then again, that means the data is in plan view if the user hits f12 for browser debug tools.现在,我可以假设将 class 消毒到 json 中,并在页面上有一个隐藏字段,但话又说回来,这意味着如果用户为浏览器调试工具点击 f12,则数据处于平面视图中。 At least ViewState is encrypted.至少 ViewState 是加密的。 Not 100% super banking like secure, but at least good enough for a set of 5 or 15 variables that my code on that page needs handy all the time in code.不是 100% 像安全的超级银行业务,但至少对于一组 5 或 15 个变量来说足够好,我在该页面上的代码在代码中一直需要得心应手。

So, let me flip the question around.所以,让我把问题反过来。

Where would you store and save the persist that set of values that a hodge podge of code behind routines that require those values to operate correctly?您将在哪里存储和保存需要这些值才能正确运行的例程背后的代码大杂烩的那组值? What is your solution?你的解决方案是什么?

My solution is to persist the class (my set of values) into ViewState.我的解决方案是将 class(我的一组值)持久化到 ViewState 中。

And as noted, I can't use session(), since then if the user has the same page open two times, then that set of class "variables" required in code will now not work, since we have two different web pages open to two different projects.如前所述,我不能使用 session(),从那时起,如果用户打开相同的页面两次,那么代码中所需的 class “变量”集现在将不起作用,因为我们打开了两个不同的 web 页面到两个不同的项目。 They thus both need their own values to work with, and for the code to operate correctly.因此,它们都需要自己的值才能使用,并且代码才能正确运行。

So, what do you propose for the above?那么,您对上述内容有何建议?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM