Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将类对象存储到会话中并显示在另一个asp.net Web窗体上_C#_Asp.net_Encapsulation - Fatal编程技术网

C# 将类对象存储到会话中并显示在另一个asp.net Web窗体上

C# 将类对象存储到会话中并显示在另一个asp.net Web窗体上,c#,asp.net,encapsulation,C#,Asp.net,Encapsulation,使用asp.net,我试图检索文本框输入,将这些数据封装在C#类中,将类对象存储在会话中,并在另一个Web表单上显示这些数据。我的想法是,我需要在C#class客户中设置属性,然后将整个类对象存储到会话中,但可能我做得不对 以下是我的asp.net代码的一部分,其中包含文本框: <asp:Label ID="lblName" runat="server" Text="Name: "></asp:Label> <br />

使用asp.net,我试图检索文本框输入,将这些数据封装在C#类中,将类对象存储在会话中,并在另一个Web表单上显示这些数据。我的想法是,我需要在C#class客户中设置属性,然后将整个类对象存储到会话中,但可能我做得不对

以下是我的asp.net代码的一部分,其中包含文本框:

  <asp:Label ID="lblName" runat="server" Text="Name: "></asp:Label>
                <br />
                <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                <br /><br />
                <asp:Label ID="lblAddress" runat="server" Text="Address: "></asp:Label>
                <br />
                <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
                <br /><br />
                <asp:Label ID="lblPhoneNumber" runat="server" Text="Phone Number: "></asp:Label>
                <br />
                <asp:TextBox ID="txtPhoneNumber" runat="server"></asp:TextBox>
这是我的课程:

这是我想显示数据的web表单:

    public partial class DisplayData : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        txtDisplayData.Text = Session["Customer"].ToString();

    }
}
目前,txtDisplayData正在显示名称空间,就是它。
有人能告诉我正确的方向吗?我觉得我想得太多了。谢谢。

您可以在会话中存储任何内容。要检索其属性,请将会话转换为原始类。它应该有效

在您的情况下,您直接提供会话,需要将其转换回类,然后提供:

Customer customer = new Customer();
// Assign object into session
Session["Customer"] = customer ;
您可以获得如下详细信息:

// get session value into object
Customer customer =  (Customer)Session["Customer"];

您可以键入casting Session[“Customer”]到客户对象,如

var obj=(客户)会话[“客户”]


txtDisplayData.Text=obj.Name

客户
需要可二进制序列化。顺便问一下,你在这段代码中遇到了什么问题?目前,它正在显示名称空间。很高兴看到我的学生在这个网站上发布他们的作业。我如何将其转换回原始类?代码解释了自己。你试过答案中写的代码了吗?通过使用
Customer=(Customer)会话[“Customer”]
您将通过
txtDisplayData.Text=customer.Name
获取您的数据。我在我的回答中提到过。嗨,BenTen。如果这个或任何答案都解决了你的问题,请点击检查标记来考虑。这向更广泛的社区表明,你已经找到了一个解决方案,并给回答者和你自己带来了一些声誉。没有义务这样做。
Customer customer = new Customer();
// Assign object into session
Session["Customer"] = customer ;
// get session value into object
Customer customer =  (Customer)Session["Customer"];