如何对Session进行判断

当从登陆页面到另一个页面时,如何去判断Session对象,以下是个简单的例子
在隐藏文件中插入以下代码
1if (Session["User"== null || Session["User"].ToString() == "")
2           {
3                Response.Write("<script language='javascript'>alert('请先登陆……');window.parent.location.href='../Default.aspx';</script>");
4            }

将以上代码打包成一个类进行调用代码如下
 1using System;
 2using System.Data;
 3using System.Configuration;
 4using System.Web;
 5using System.Web.SessionState;
 6using System.Web.Security;
 7using System.Web.UI;
 8using System.Web.UI.WebControls;
 9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11
12/// <summary>
13/// Cdata 的摘要说明
14/// </summary>

15public class Cdata : System.Web.UI.Page       //只要从   System.Web.UI.Page   类继承的子类,都可以直接引用   Session
16{
17    public Cdata()
18    {
19        //
20        // TODO: 在此处添加构造函数逻辑
21        //
22    }

23       public string  SessionCheck()
24    {
25        
26        string checkstr = "";
27      
28            if (Session["User"== null || Session["User"].ToString() == "")
29            {
30              checkstr="<script language='javascript'>alert('请先登陆……');window.parent.location.href='../Default.aspx';</script>";
31            }

32            return checkstr;
33       
34    }

35 
36}

37
接下来在隐藏文件中调用此方法
 1 protected void Page_Load(object sender, EventArgs e)
 2    {
 3
 4        if (!Page.IsPostBack)
 5        {
 6            Cdata cd = new Cdata();
 7            string chestr=cd.SessionCheck();
 8            Response.Write(chestr);
 9            //if (Session["User"] == null || Session["User"].ToString() == "")
10            //{
11            //    Response.Write("<script language='javascript'>alert('请先登陆……');window.parent.location.href='../Default.aspx';</script>");
12            //}
13        }

14
15    }
posted @ 2008-05-09 01:30  冷风醉饮  阅读(1091)  评论(0)    收藏  举报