简体   繁体   English

如何将TabContainer保存到Viewstate?

[英]How to save TabContainer to Viewstate?

I've been struggling with saving a TabContainer to a viewstate variable that would load all of the TabPanels of that TabContainer. 我一直在努力地将TabContainer保存到一个viewstate变量中,该变量将加载该TabContainer的所有TabPanel。 (Would page be loaded faster?). (页面加载速度会更快吗?)。 Pardon me if the question is not properly formatted; 如果问题的格式不正确,请原谅我。 I'm still new to asp.net, here's my code: 我还是asp.net的新手,这是我的代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        CreateTabbedPanel();
        Response.Write("New Tabs Loaded");
    }
    else
    {
        // Here i would need to load the TabContainer from the viewstate variable
        Response.Write("Tabs Loaded from ViewState");
    }
}

private void CreateTabbedPanel()
{
    TabPanel tp = null;

    string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    using (SqlConnection con = new SqlConnection(CS))
    {
        SqlCommand cmd = new SqlCommand("select Description from TblProductType", con);
        con.Open();
        SqlDataReader rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            tp = new TabPanel();
            tp.HeaderText = rdr["Description"].ToString();                    
            TabContainer1.Tabs.Add(tp);
        }
    }
    // Here i would need to create the viewstate variable
}

And this is my webform: 这是我的网络表单:

<form id="form1" runat="server">
    <div>
        <ajaxToolkit:TabContainer ID="TabContainer2" runat="server" 
           ActiveTabIndex="1" AutoPostBack="false">
        </ajaxToolkit:TabContainer>
    </div>
</form>

What do I need to do? 我需要做什么?

If you wish to save a .Net object to ViewState, you need to make sure that the object supports serialization/deserialization. 如果希望将.Net对象保存到ViewState,则需要确保该对象支持序列化/反序列化。 However, this unlikely increase performance. 但是,这不太可能提高性能。

ViewState data is rendered into a specific hidden field. ViewState数据呈现到特定的隐藏字段中。 If you save complex objects into ViewState, you greatly increase HTML page size. 如果将复杂对象保存到ViewState中,则会大大增加HTML页面大小。 Next, serialization/deserialization also takes time. 接下来,序列化/反序列化也需要时间。

And finnaly, you cannot store ASP.NET control in ViewState because of ASP/NET page and control life cycle . 最后,由于ASP / NET页面和控件的生命周期 ,您无法在ViewState中存储ASP.NET控件。 When yo create and add a control into a page control hierarchy, it goes through all stages of ASP.NET life cycle: Init, Load, Render, [saving in ViewSate], Unload. 当您创建控件并将其添加到页面控件层次结构中时,它将经历ASP.NET生命周期的所有阶段:初始化,加载,渲染,[保存在ViewSate中],卸载。 When you get the control from the ViewState next time, the control state will be 'after-render'. 下次当您从ViewState获取控件时,控件状态将为“渲染后”。 If you add such control into a new page control hierarchy, the control in 'after-render' state starts going through Init, Load and Render stages again. 如果将此类控件添加到新的页面控件层次结构中, 则处于“渲染后”状态的控件将再次开始执行Init,Load和Render阶段。 Although it can operate in the case of primitive controls (Labes, TextBoxes, etc.), it totally breaks control life cycle and causes different strange issues in complex controls. 尽管它可以在原始控件(Labes,TextBoxes等)的情况下运行,但它完全破坏了控件的生命周期,并在复杂控件中引起了不同的奇怪问题。

If you create ASP.NET controls dynamically, I strongly recommend you re-create them on every new page request as ASP.NET page life cycle requires . 如果您动态创建ASP.NET控件, 强烈建议您按照ASP.NET页面生命周期的要求,在每个新页面请求上重新创建它们

Assuming you only need to save few tab names, you can use ViewState . 假设您只需要保存几个选项卡名称,则可以使用ViewState Otherwise I'd recommend Session as it allows to store complex objects at server side without having to post them into the page, it takes less code but you also need to handle session expired. 否则,我建议使用Session因为它允许在服务器端存储复杂的对象而不必将它们发布到页面中,它需要较少的代码,但是您还需要处理过期的会话。

The collection of tabs is read-only, non serializable, etc. However this code can be used to save headers in CreateTabbedPanel() . 选项卡的集合是只读的,不可序列化的等。但是,此代码可用于将标头保存在CreateTabbedPanel()

private void SaveTabs()
{
    TabPanel[] tabs = new TabPanel[TabContainer1.Tabs.Count];
    TabContainer1.Tabs.CopyTo(tabs, 0);
    ViewState["tabs"] = tabs.Select(t=>t.HeaderText).ToArray();
}

When page load is not post back: 如果页面加载没有回发:

private void LoadTabs()
{
    string[] headers = (string[])ViewState["tabs"];
    if(headers!=null)
    {
        foreach (string header in headers)
        {
            TabContainer1.Tabs.Add(new TabPanel() { HeaderText = header });
        }
    }
}

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

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