将Class对象存储到Session中并在另一个asp.net Webform上显示

问题描述 投票:1回答:2

使用asp.net,我试图检索文本框输入,将此数据封装在C#类中,将类对象存储在会话中并在另一个Web窗体上显示此数据。我的想法是我需要在C#类Customer中设置属性,然后将整个类对象存储到Session中,但是我可能会犯这个错误。

这是我的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>

这是我的代码背后:

protected void btnSubmitPersonalInfo_Click(object sender, EventArgs e)
        {
            Customer customer = new Customer();
            customer.Name = txtName.Text;
            customer.Address = txtAddress.Text; 
            customer.PhoneNumber = txtPhoneNumber.Text;
            customer.City = txtCity.Text;
            customer.State = txtState.Text; 
            customer.Zip = txtZip.Text;

            Session["Customer"] = customer;
            Response.Redirect("CreditCardInfo.aspx");
        }

这是我的C#类:

public class Customer
    {
        public string Name { get; set; }
        public string Address { get; set; }
        public string PhoneNumber { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zip { get; set; }
        public string CardNumber { get; set; }
        public string ExpirationDate { get; set; }
        public string SecurityCode { get; set; }
        public Customer() { }

    }    

这是我要显示数据的Web表单:

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

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

    }
}

目前,txtDisplayData正在显示命名空间,就是这样。有人可以指出我正确的方向,因为我觉得我正在思考这个问题。谢谢。

c# asp.net encapsulation
2个回答
© www.soinside.com 2019 - 2024. All rights reserved.