将控件序列化为视图状态

发布于 2024-10-16 13:45:24 字数 189 浏览 0 评论 0原文

我正在编写一个自定义用户控件。我觉得我正在以艰难的方式做这件事。

据我了解,为了在回发之间保留控件的状态,我必须将数据保存到 ViewState。我已经在我的用户控件类中的几个字段中完成了此操作。

然而,这似乎非常乏味。有没有办法让 ASP.net 在页面加载完成后立即将用户控件中的所有可序列化字段保存到 ViewState 中?

I'm writing a custom user control. I feel like I'm doing this the hard way.

I understand that in order for my control's state to be preserved between postbacks, I have to save the data to ViewState. I've done this with several fields in my User Control's class.

However, it seems very tedious. Is there a way to make ASP.net save all the Serializable fields in my User Control to ViewState all at once when the page is done loading?

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

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

发布评论

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

评论(1

枫林﹌晚霞¤ 2024-10-23 13:45:24

我有一个解决方案,您仍然需要根据您的情况进行调整,因为此示例(重新)存储了控件的所有状态,以及 asp.net 运行时设置的属性。请记住,序列化不能在字段的属性上设置,只能在类/结构上设置。但是,您可以创建自己的属性(ViewStateSerialized?),用于装饰要在回发期间保留的属性。请记住,视图状态是通过线路传输到客户端的,因此如果您有很多视图状态,用户可能会感到不安......

protected override object SaveViewState()
{
    Dictionary<string, object > dict = new Dictionary<string, object>();
    foreach (var prop in this.GetType().GetProperties())
    {
        // here we decide what to save
        if (prop.PropertyType.GetCustomAttributes(
              typeof(SerializableAttribute), false).Length>0)
        {
            dict.Add(prop.Name, prop.GetValue(this, new object[] {}));
        }
    }

    var ms = new MemoryStream();
    BinaryFormatter bf = new BinaryFormatter();
    bf.Serialize(ms, dict);

    return ms.ToArray();
}


protected override void LoadViewState(object savedState)
{
    BinaryFormatter bf = new BinaryFormatter();
    Dictionary<string, object> dict = 
        (Dictionary<string, object>) bf.Deserialize(
        new MemoryStream((byte[])savedState));

    foreach(var kv in dict)
    {
        this.GetType()
            .GetProperty(kv.Key)
            .SetValue(this, kv.Value, new object[] {});
    }
    base.LoadViewState(savedState);
}

I have a solution that you still need to adapt for your situation because this sample (re)stores all state of the control, also properties set by the asp.net runtime. Remember that Serializable is not settable on a property of field, only a class/struct. You can however create your own Attribute (ViewStateSerializable?) that you use to decorate the properties you want to retain during postbacks. Do remember that viewstate is going over the wire to the client so if you have a lot of it users might get upset....

protected override object SaveViewState()
{
    Dictionary<string, object > dict = new Dictionary<string, object>();
    foreach (var prop in this.GetType().GetProperties())
    {
        // here we decide what to save
        if (prop.PropertyType.GetCustomAttributes(
              typeof(SerializableAttribute), false).Length>0)
        {
            dict.Add(prop.Name, prop.GetValue(this, new object[] {}));
        }
    }

    var ms = new MemoryStream();
    BinaryFormatter bf = new BinaryFormatter();
    bf.Serialize(ms, dict);

    return ms.ToArray();
}


protected override void LoadViewState(object savedState)
{
    BinaryFormatter bf = new BinaryFormatter();
    Dictionary<string, object> dict = 
        (Dictionary<string, object>) bf.Deserialize(
        new MemoryStream((byte[])savedState));

    foreach(var kv in dict)
    {
        this.GetType()
            .GetProperty(kv.Key)
            .SetValue(this, kv.Value, new object[] {});
    }
    base.LoadViewState(savedState);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文