简体   繁体   English

每个变量的ViewState与一个变量

[英]ViewState per variable vs One variable

I am using viewstate quite a lot to store some flags and small data that needs to be persisted over the postbacks. 我经常使用viewstate来存储一些标志和小数据,这些标志和小数据需要在回发中持久化。 Most of the time I use it like so 大部分时间我都这样使用

public partial class testPage : System.Web.UI.Page
{
    public string item1 { get { return (string)(ViewState["item1"] ?? default(string)); } set { ViewState["item1"] = value; } }
    public string item2 { get { return (string)(ViewState["item2"] ?? default(string)); } set { ViewState["item2"] = value; } }
    public string item3 { get { return (string)(ViewState["item3"] ?? default(string)); } set { ViewState["item3"] = value; } }
    public bool flag1 { get { return (bool)(ViewState["flag1"] ?? default(bool)); } set { ViewState["flag1"] = value; } }
    public bool flag2 { get { return (bool)(ViewState["flag2"] ?? default(bool)); } set { ViewState["flag2"] = value; } }   

    protected void Page_Load(object sender, EventArgs e)
    {
       // flag1...
    }
}

This is fine but looks a bit heavy and I'm not sure whether the desirialization process takes place only once or each time I access the variable. 很好,但看起来有点沉重,我不确定是否需要一次或每次访问变量时进行反序列化过程。 So I thought may be this technique is a bit better in terms of performance and management 所以我认为这种技术在性能和管理方面可能会更好

public partial class testPage : System.Web.UI.Page
{
    public PageVeriables Variables { get { return (PageVeriables)(ViewState["Variables"] ?? default(PageVeriables)); } set { ViewState["Variables"] = value; } }    

    protected void Page_Load(object sender, EventArgs e)
    {
      //Variables.flag1..
    }
}

[Serializable]
public class PageVeriables
{
    public string item1 { get; set; }
    public string item2 { get; set; }
    public string item3 { get; set; }
    public bool flag1 { get; set; }
    public bool flag2 { get; set; }
}

Question is does one really have any benefit over the other? 问题是,一个真的比另一个有什么好处吗?

I would recommend doing the first example, even though it might be more typing. 我建议您做第一个示例,即使它可能需要更多输入。

It has two distinct advantages: 它具有两个明显的优点:

The first being what Michael Liu stated, which is that the size of the ViewState is has more granularity. 第一个是Michael Liu所说的,那就是ViewState的大小具有更高的粒度。 If you only use 2 of 10 properties, then your ViewState size only accounts for the 2 that you've actually assigned to. 如果仅使用10个属性中的2个,则ViewState大小仅占您实际分配给的2个属性。

The second which I feel like even more important is that this follows the same pattern of UserControls, in that they have properties that you assign to. 我觉得更重要的第二点是,它遵循UserControls的相同模式,因为它们具有您分配的属性。 For example, given a UserControl with a Text property, using the user control in the ASPX would look like the following: 例如,给定一个具有Text属性的UserControl,在ASPX中使用该用户控件应如下所示:

<demo:MyUserControl Text="Some Text" runat="server" />

Which will also work in the designer. 这也将在设计器中工作。 So there's something to be said for following convention, that (for the most part) properties get persisted between post-backs. 因此要遵循以下约定,在大多数情况下,属性在回发之间保持不变。

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

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