存储面板控件的内容到一个cookie [英] Store the Contents of a Panel Control into a Cookie

查看:91
本文介绍了存储面板控件的内容到一个cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个网页,它允许用户显示或隐藏它通过显示/隐藏按钮的内容部分的选项。单击显示/隐藏按钮后,根据选定的,那么用户可以通过点击保存按钮保存页面按钮嵌套面板/内容变得可见或不可见。问题 - (没有错误),但是,页面不保存用户变成饼干。该页面包含嵌套在一个主面板控制2面板控制。

I created a page, which allows users the option to show or hide it's content sections via a show/hide button. After clicking the show/hide button, the nested panels/content becomes visible or invisible based on the button selected, then the user may save the page by clicking a save button. Problem - (no errors) but, the page is not saving the users changes into the cookie. The page contains 2 panel controls that are nested in one main Panel control.

//Front End code - The save button
<asp:Button ID="savButton" runat="server" Text="Save" onclick="savButton_Click" />

//psuedo code - The Panels
<asp:Panel ID="pnlSaveContent" runat="server"> //main Panel control 
     <asp:Panel ID="pnlWeatherAppCtrl" runat="server"> // panel content 1
         <div>Weather App Content</div>
     </Panel>
     <asp:Panel ID="StockAppCtrl" runat="server">   // panel content 2
           <div>Stock App Content</div>
      </Panel>
</Panel>

//Back-end code:
    protected void Page_Load(object sender, EventArgs e)
     {
        //get the cookie       
        if ((Request.Cookies["preferences"] != null))
        {
            pnlSaveContent.ID = Request.Cookies["preferences"]["savePg"];
        }
     }

 //set cookie
    protected void savButton_Click(object sender, EventArgs e)
    {
        Response.Cookies["preferences"]["savePg"] = pnlSaveContent.ID;
        Response.Cookies["preferences"].Expires = DateTime.MaxValue;
    }

 //end code

...问题:页面不保存主面板控制的变化。可能有人请提供一些指导,我做错了什么?

...the issue: The page is not saving the changes of the main panel control. Could someone please provide some guidance as to what I’m doing wrong?

推荐答案

不要忘了保存cookie的 Response.Cookies.Add

Don't forget to save the cookie with Response.Cookies.Add:

protected void savButton_Click(object sender, EventArgs e)
{
    HttpCookie c = Request.Cookies["preferences"] != null ? 
        Request.Cookies["preferences"] :
        new HttpCookie("preferences");
    c.Values["savePg"] = pnlSaveContent.ID;
    c.Expires = DateTime.MaxValue;
    Response.Cookies.Add(c);
}

至于您的评论...我不太知道你正在尝试做的,但也许是这样的。这将取决于cookie的值设置面板的可见性(能见度如果ID cookie的值相匹配)。

As for your comment... I'm not quite sure what you are trying to do, but maybe it is this. This will set the visibility of the panel depending on the value of the cookie (visibility is false if the ID matches the value of cookie).

protected void Page_Load(object sender, EventArgs e)
{
    //get the cookie       
    if ((Request.Cookies["preferences"] != null))
    {
        pnlSaveContent.Visible = !(pnlSaveContent.ID == Request.Cookies["preferences"]["savePg"]);
    }
}

这篇关于存储面板控件的内容到一个cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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