使用会话变量加载页面时显示数据 [英] To displaydata when the page is loaded using session variables

查看:47
本文介绍了使用会话变量加载页面时显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一个人登录到他的页面时,他的所有详细信息都默认显示在页面上,即在页面加载本身期间.我需要使用会话必须在页面加载下编写的代码,请帮助我

when a person has loged in he''s directed to a page in which all his details are present defaultly i.e during the page load itself.i need the code that has to be written under the page load using sessions please help me out.

推荐答案

首先创建一个具有数据成员的可序列化类,该成员实际上代表已登录人员的数据.实例化该类,将数据值放入并序列化到Session中.在目标页面中,反序列化对象并将数据成员值绑定到特定控件.

类似于以下代码:

1.创建一个可序列化的类

First make a serializable class with data members which actually represents the data of the logged in person. Instantiate the class, put the data values and Serialize it in Session. In the target page deserialize the object and bind the data members values to specific controls.

Something like the following piece of code:

1. Create a serializable class

[Serializable]
public class UserData
{
   private string _userFirstName;
   prvate string _userLastName;
   ....................
   ....................
   public string FirstName
   {
    get{return this._userFirstName;}
    set{this._userFirstName = value;}
   }
   .................................
   ...................
}



2.序列化Login.aspx.cs类中的类



2. Serialize the class in the Login.aspx.cs class

if(Authenticated)
{
  UserData objUserData = new UserData();
  objUserData.FirstName = "John";
  objUserData.LastName = "Scott";
  ..............................
  ................................
 Session.Add("UserData",objUserData);
}



3.从Session中反序列化对象,然后将值绑定到目标页面的Load方法中的属性.



3. Deserialize the object from Session and bind the values to the property in the targeted pages Load method.

protected void Page_Load(object sender, EventArgs e)
{
   UserData objUserData = (UserData)Session["UserData"];
   lblUserFirstName.Text = objUserData.FirstName;
   lblUserLastName.Text = objUserData.LastName;
   .............................................
   ........................................
}



希望对您有帮助.



Hope this helps you.


这篇关于使用会话变量加载页面时显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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