首页 > 代码库 > 使用 IntraWeb (40) - 自定义 Session 数据

使用 IntraWeb (40) - 自定义 Session 数据


修改 UserSessionUnit 单元:
unit UserSessionUnit;interfaceuses  IWUserSessionBase, SysUtils, Classes, IWApplication;type  TIWUserSession = class(TIWUserSessionBase)    procedure IWUserSessionBaseCreate(Sender: TObject);    procedure IWUserSessionBaseDestroy(Sender: TObject);  private    FMyName: string;    FMyAge: Integer;    FMyInfos: TStrings;  public    property MyName: string read FMyName write FMyName;    property MyAge: Integer read FMyAge write FMyAge;    property MyInfos: TStrings read FMyInfos write FMyInfos;  end;implementation{$R *.dfm}procedure TIWUserSession.IWUserSessionBaseCreate(Sender: TObject);begin  FMyInfos := TStringList.Create;end;procedure TIWUserSession.IWUserSessionBaseDestroy(Sender: TObject);begin  FMyInfos.Free;end;end.


测试从 Unit1 写入数据:
uses ServerController, Unit2;procedure TIWForm1.IWButton1AsyncClick(Sender: TObject; EventParams: TStringList);begin  UserSession.MyName := ‘aaa‘;  UserSession.MyAge := 111;  UserSession.MyInfos.Add(‘Var1=123‘);  UserSession.MyInfos.Add(‘Var2=456‘);  TIWForm2.Create(WebApplication).Show;end;


测试从 Unit2 读取数据:
uses ServerController;procedure TIWForm2.IWButton1AsyncClick(Sender: TObject; EventParams: TStringList);begin  IWMemo1.Lines.Add(UserSession.MyName);  IWMemo1.Lines.Add(UserSession.MyAge.ToString());  IWMemo1.Lines.Add(UserSession.MyInfos.Values[‘Var1‘]);  IWMemo1.Lines.Add(UserSession.MyInfos.Values[‘Var2‘]);end;