将 webControls 保存在 Session 对象中

发布于 2025-01-03 16:43:48 字数 416 浏览 0 评论 0原文

我有一个面板 (pnlPanel),其中包含许多控件,例如 Textboxes 和 DropDownLists。我希望它们在用户返回页面时保持不变,所以我尝试了以下方法:

/*i have saved the panel like this 
  Session["testPanel"] = pnlTest;
*/

protected void Page_Load(object sender, EventArgs e)
{       
    if (Session["testPanel"] != null)
    {
        panel = Session["testPanel"] as Panel;
    }
}

但它不起作用。是否可以?我之所以要这样做是因为开销不是问题,而且我想减少编码时间。

I have a panel (pnlPanel) with lots of controls like Textboxes and DropDownLists. I want them to be persistent when the user gets back to the page, so i tried this:

/*i have saved the panel like this 
  Session["testPanel"] = pnlTest;
*/

protected void Page_Load(object sender, EventArgs e)
{       
    if (Session["testPanel"] != null)
    {
        panel = Session["testPanel"] as Panel;
    }
}

But its not working. Is it possible? The reason why i want to do this is because overhead is not a problem, and i want to cut down on coding time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

北笙凉宸 2025-01-10 16:43:48

我自己从未尝试过,但在我看来,这是一个非常糟糕的主意。如果没有测试它,我的猜测是这会产生大量的 ViewState 问题。即使您可以维护 ViewState,尝试对多个页面加载保持这种控制充其量也是危险的。

我的建议是使用一个通用对象来保存所需面板的属性,然后在早期事件之一中构建一个方法,以使用这些属性预填充新面板。

I've never tried this myself, but this seems to me to be an extra-ordinarily bad idea. Without testing it, my guess would be that this will create a ton of ViewState problems. Even if you could maintain the ViewState, attempting to keep this control over multiple page loads would be dangerous at best.

My recommendation would be to have a common object that holds the properties of the panel you want and just build a method into one of the early events to prepopulate a new panel with those properties.

萌无敌 2025-01-10 16:43:48

在不知道执行此类操作的全部原因的情况下,您应该查看输出缓存指令。您最好将内容从面板中拉出并放入用户控件中。然后使用 VaryByCustom 在控件上设置输出缓存,以便您可以使用用户名或其他唯一标识符来按用户分隔。

http://msdn.microsoft.com/en-us/library/hdxfb6cy.aspx
http://msdn.microsoft.com/en-us /library/system.web.httpapplication.getvarybycustomstring.aspx

如果您处于 webfarm 场景中,使用会话和/或缓存将会出现问题。缓存的范围仅限于应用程序实例,因此网络场中的其他服务器将无法访问它。

类似的一些其他副作用包括视图状态问题。

Without knowing the entire reason for doing something like this, you should have a look at output caching directives. You would be best served by pulling the content out of the panel and into a user control. Then setting output caching on control, using VaryByCustom so you can use the user name or some other unique identifier to separate by user.

http://msdn.microsoft.com/en-us/library/hdxfb6cy.aspx and
http://msdn.microsoft.com/en-us/library/system.web.httpapplication.getvarybycustomstring.aspx

Using session and/or caching will be problematic if you are in a webfarm scenario. Cache is scoped to the application instance, so another server in the web farm will not have access to it.

Some other side effects of something like this include issues with viewstate.

眉目亦如画i 2025-01-10 16:43:48

您在这里尝试做的是缓存面板,但这不是方法。保存时的面板是内存中的运行对象,无法按原样保存。您需要将其转换为 html 字符串并保存并缓存该字符串。因此,在面板附近放置一个文字,然后渲染面板并将其保存在会话中,然后实际上显示此渲染中的文本。

if(Session["testPanel"] == null)
{
    TextWriter stringWriter = new StringWriter();
    HtmlTextWriter renderOnMe = new HtmlTextWriter(stringWriter);
    // render and get the actually html of this dom tree
    testPanel.RenderControl(renderOnMe);
    // save it as cache
    Session["testPanel"] = stringWriter.ToString();     
}
// render the result on a literal 
cLiteralID.Text = Session["testPanel"];
// hide the panel because I have render it on literal.
testPanel.Visible = false;

需要一些测试。我对自定义控件和自定义缓存使用一些类似的代码,从不在会话中保存这么多数据。

What you try to do here is to cache the Panel but this is not the way. The panel as you save it is a running object on the memory and can not be saved as it is. You need to convert it to html string and save and cache this string. So near the Panel you place a literal, then you render the Panel and save it on session, and then actually you display the text from this render.

if(Session["testPanel"] == null)
{
    TextWriter stringWriter = new StringWriter();
    HtmlTextWriter renderOnMe = new HtmlTextWriter(stringWriter);
    // render and get the actually html of this dom tree
    testPanel.RenderControl(renderOnMe);
    // save it as cache
    Session["testPanel"] = stringWriter.ToString();     
}
// render the result on a literal 
cLiteralID.Text = Session["testPanel"];
// hide the panel because I have render it on literal.
testPanel.Visible = false;

Need some tests as is it. I use some similar code for custom control and custom cache, never save on session this amount of data.

习ぎ惯性依靠 2025-01-10 16:43:48

第一种方法


protected void Page_Load(object sender, EventArgs e)
{       
    if (ViewState["panel"] != null)
    {
        panel = ViewState["panel"] as Panel;
    }
}

在这种方法中,您的 ViewState 对象是不同的。一旦为 ViewState["panel"] 提供了控制内存并且正在访问该对象,您可能会得到一些 null 值,感觉会话是 Session["panel"]< /code>


第二种方法


完整面板 HTML 保存在数据库中,并通过将函数保留在 IsPostBack< 下,在表单加载上访问它/代码>。

现在,继续使用方法 - 2 将值分配给您的会话对象。

this.Controls.Add(new LiteralControl("Your HTML"));

第三种方法


您可以使用文件系统。将 div 保存在文件中并在运行时访问该文件。

希望这可以帮助你。


编辑 - 1 =>添加了第二种方法的代码

First Approach


protected void Page_Load(object sender, EventArgs e)
{       
    if (ViewState["panel"] != null)
    {
        panel = ViewState["panel"] as Panel;
    }
}

In this approach your ViewState objects were different. You may be getting some null values once the ViewState["panel"] is given the control memory and the object is being accessed in the impression that the Session was Session["panel"]


Second Approach


Save the Complete panel HTML in database and access it on the form load by keeping the function under IsPostBack.

Now with the continuity of approach - 2 assign the value to your session object.

this.Controls.Add(new LiteralControl("Your HTML"));

Third Approach


You can use File system. Save the div in your file and access the file at runtime.

Hope this may help you.


EDIT - 1 => Added code for second approach

那伤。 2025-01-10 16:43:48

我有类似的问题。我尝试将对象保存到存储面板的视图状态,但收到一条错误消息,告诉我面板不可序列化。您可以尝试使用 SerializationSurrogate。
https://msdn。 microsoft.com/en-us/library/system.runtime.serialization.serializationsurrogate(v=vs.110).aspx

I had a similar problem. I tried to save an object to the View State that stored a Panel and I got an error message telling me that Panels aren't serializable. You could try using a SerializationSurrogate.
https://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializationsurrogate(v=vs.110).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文