在会议声明中ASPX脚本块一类然后再将其存储 [英] Declaring a Class in ASPX Script Block Then Storing it in Session

查看:84
本文介绍了在会议声明中ASPX脚本块一类然后再将其存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法这样做,我觉得应该是非常可行的东西。也许有人可以告诉我,我错过了什么。
我仅限于加入我的C#code到我的.aspx页的服务器端脚本块(无code后面)。我想(不会造成任何其它文件再次)在ASPX页面上定义一个新类(这将是一个嵌套类),然后存储在会话这些对象的列表回发之间,并且用户后坚持上花费一些时间其他页面,然后返回到所述一个问题。
这里是我的code ...

I am having trouble doing something that I think should be very doable. Perhaps someone can tell me what I'm missing. I am restricted to adding my C# code to my .aspx page in a server-side script block (no code behind). I would like to define a new class (again without creating any other files) on the ASPX page (this would be a nested class) and then store a List of these objects in Session to persist between postbacks and after the user spends some time on other pages and then returns to the one in question. Here's my code...

    <script language="C#" runat="Server">
    protected List<Part> AssignmentSearchResults
    {
        get
        {
            if (Session["SearchResults"] == null)
                Session["SearchResults"] = new List<Part>();
            return (List<Part>)Session["SearchResults"];
        }
        set
        {
            Session["SearchResults"] = value;
        }
    }

    public class Part
    {
        public string Id { get; set; }
        public string Description { get; set; }
        public string Type { get; set; }
        public bool Selected { get; set; }
    }

</script>
(html goes here)

如果我用清单只是正常工作,但是当我用我自定义的类没有。以下是错误我得到...

If I use List it works just fine, but when I use my custom class it does not. Here is the error I get...

[A] System.Collections.Generic.List 1 [ASP.pub_usercontrols_exeplanning_resources_ascx +部分]不能转换为[B] System.Collections.Generic.List 1 [ ] ASP.pub_usercontrols_exeplanning_resources_ascx +部分。类型从起源'mscorlib程序,版本= 4.0.0.0,文化=中性公钥= b77a5c561934e089'在位置'C上下文'LoadNeither':\\ WINDOWS \\ Microsoft.Net \\装配\\ GAC_32 \\ mscorlib程序\\ v4.0_4.0.0 .0__b77a5c561934e089 \\ mscorlib.dll中'。 B型从起源'mscorlib程序,版本= 4.0.0.0,文化=中性公钥= b77a5c561934e089'在位置上下文LoadNeither''C:\\ WINDOWS \\ Microsoft.Net \\装配\\ GAC_32 \\ mscorlib程序\\ v4.0_4.0.0 .0__b77a5c561934e089 \\ mscorlib.dll中'。

[A]System.Collections.Generic.List1[ASP.pub_usercontrols_exeplanning_resources_ascx+Part] cannot be cast to [B]System.Collections.Generic.List1[ASP.pub_usercontrols_exeplanning_resources_ascx+Part]. Type A originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Type B originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'.

......也许你会注意到,这两个(A和B)是完全一致的。
有任何想法吗?非常感谢您事先的任何帮助。

...and perhaps you'll note that the two (A and B) are completely identical. Any ideas? Thanks a lot in advance for any help.

推荐答案

我不知道是什么原因造成的问题,但我有一个解决方案。
由于会话对象正在反正系列化,为什么不先序列化对象的对象/列表,然后把它放到会议[]?

I don't know what is causing your problem, but I have a solution. Since session objects are being serialized anyway, why not serialize the object/list of objects first and then put it into Session[]?

IE:

List<Part> parts = new List<Part>();
//... populate parts
XmlSerializer xs = new XmlSerializer(typeof(List<Part>));

MemoryStream ms = new MemoryStream();
xs.Serialize(ms, parts);

// Rewind the stream and write it to session as XML

ms.Seek(0, SeekOrigin.Begin);
Session["XMLResults"] = Encoding.ASCII.GetString(ms.ToArray());

//Get memory stream from session
ms = new MemoryStream();
byte[] bData = Encoding.ASCII.GetBytes(Session["XMLResults"].ToString());
ms.Write(bData, 0, bData.Length);

// Hydrate parts list from a memory stream
ms.Seek(0, SeekOrigin.Begin);
parts = (List<Part>)xs.Deserialize(ms);

这篇关于在会议声明中ASPX脚本块一类然后再将其存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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