保持视图状态持久性和检索需求 [英] Keeping the Viewstate persistent and retrieve it on demand

查看:144
本文介绍了保持视图状态持久性和检索需求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面有几个控制,如Checkboxlists和两个集装箱控股搜索结果。

I've got a page with several controls, such as Checkboxlists and two containers holding search results.

有人告诉我做这个网站执着,使用户可以从任何地方回来在浏览我们的网站,发现它是恢复设置(读:箱stll检查,结果仍然可用)

I've been told to make this site persistent, so the user can come back from anywhere while browsing our site and find it's settings restored (read: boxes stll checked; results still available)

到目前为止,我已经尝试重写SavePageStateToPersistenceMedium和LoadPageStateFromPersitenceMedium,但我一直运行到死亡的黄色画面不久,当我点击页面上的按钮。

So far I have tried to override SavePageStateToPersistenceMedium and LoadPageStateFromPersitenceMedium, but I am constantly running into a yellow screen of death as soon, as I click a button on the page.

protected override void SavePageStateToPersistenceMedium(object viewState)
{
    string str = "VIEWSTATE_" + Request.UserHostAddress;
    Cache.Add(str, viewState, null, DateTime.Now.AddMinutes(Session.Timeout), TimeSpan.Zero, CacheItemPriority.Default, null);
    ClientScript.RegisterHiddenField("__VIEWSTATE_KEY", str);
    ClientScript.RegisterHiddenField("__VIEWSTATE", "");
}
protected override object LoadPageStateFromPersistenceMedium()
{
    string str = Request.Form["__VIEWSTATE_KEY"];
    if (!str.StartsWith("VIEWSTATE_"))
    {
        throw new Exception("Invalid viewstate key:" + str);
    }
    return Cache[str];
}

这有什么错我的code和我需要调用来检索的Page_Load或Page_init视图状态怎么找回?

Is there anything wrong with my code and what would I need to call to retrieve the Viewstate on Page_Load or Page_init to retrieve it?

编辑:
这是通过会议来解决它的办法[XYZ]

edit: Here is the approach to solve it via session["xyz"]

List<ListItem> selectItems = new List<ListItem>();
        foreach (ListItem item in CheckBoxListLevel.Items)
        {
            if (item.Selected)
                selectItems.Add(item);
        }

        Session.Add("SavedLevelItems", selectItems);

//然后在Page_Load中

//And then in Page_Load

   if (Session["SavedLevelItems"] != null)
    {
        List<ListItem> SessionList = (List<ListItem>)Session["SavedLevelItems"];
        foreach (var item in SessionList)
        {
            if (item.Selected)
            {
                CheckBoxListLevel.Items.FindByText(item.Text).Selected = item.Selected;
            }
        }
    }

不过,如果我查看信息搜索结果,然后就触发一个GET,回到初始页面,所有存储的项目不正确呈现。

However, if I view a searchresult and then just fire a GET to get back to the initial page, all stored items are not rendered correctly.

推荐答案

解决方案是pretty到底简单。
我只是需要一些控制,而不是整个页面的状态,因为结果可能会随时更改。

The solution was pretty simple in the end. I only needed the state of some controls and not the whole page, since the results could change anytime.

问题是,这对CheckBoxLists的数据源是在aspx文件中定义。
这导致加载没有任何项目时,我试图通过的Page_Load()。

The problem was, that the datasources for the CheckBoxLists was defined in the aspx file. this resulted in no items being loaded when I tried to access the controls via Page_Load().

所以我转了整个数据访问逻辑隐藏文件code,包装在一个if语句。如果一个合适的会话对象可在数据源是会话对象,把它从其他数据库。

So i transferred the whole data access logic to the code behind file, wrapped in an if statement. If a fitting session object is available the datasource is the session object, else take it from the database.

 protected void fillEntrylevelList()
{
    using (OleDbConnection connection = new OleDbConnection(ConfigurationManager.ConnectionStrings["TestEnvironment"].ConnectionString))
    {
        string EntrylevelQuery = @"INSERT SQL STATEMENT HERE";
        OleDbCommand command = new OleDbCommand(EntrylevelQuery, connection);
        if (Session["SavedLevelItems"] != null)
            {
                CheckBoxListLevel.Items.Clear();
                List<ListItem> SessionList = (List<ListItem>)Session["SavedLevelItems"];
                foreach (var item in SessionList)
                {
                    try
                    {
                        CheckBoxListLevel.Items.Add(item);
                    }
                    catch { }

                }
            }
        else
        {
            connection.Open();
            CheckBoxListLevel.DataTextField = "bez_level";
            CheckBoxListLevel.DataValueField = "id_level";
            OleDbDataReader ListReader = command.ExecuteReader();
            CheckBoxListLevel.DataSource = ListReader;
            CheckBoxListLevel.DataBind();
            ListReader.Close(); ListReader.Dispose();
            GC.Collect();
        }
    }
}

亲切的问候

这篇关于保持视图状态持久性和检索需求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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