C# 页面基类

using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  System.ComponentModel;
using  System.IO;
using  System.Text;
using  System.Text.RegularExpressions;
using  System.Collections;

namespace  UserManager
{
    [DesignerCategory(
" ASPXCodeBehind " )]
    
public   class  PageBase : System.Web.UI.Page
    {
        
protected   override   void  OnLoad(EventArgs e)
        {
            Context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            
/// /首次加载,检查权限;再次加载,检查是否登录或超时
               if  ((CheckPermission()  &&   ! IsPostBack)  ||  (IsPostBack  &&  Session[ " loginuser_id " !=   null ))
            {
                
base .OnLoad(e);
            }
            
if  ( ! this .IsPostBack)
            {
                
base .OnLoad(e);
            }
        }

        
///   <summary>
        
///  将对象保存到Session中
         
///   </summary>
        
///   <param name="name"> 键名 </param>
        
///   <param name="value"> 键值 </param>        
         protected   virtual   void  SaveToSession( string  name,  object  value)
        {
            
if  (Session[name]  ==   null )
            {
                Session.Add(name, value);
            }
            
else
            {
                Session[name] 
=  value;
            }
        }

        
///   <summary>
        
///  将对象从Session中移出
         
///   </summary>
        
///   <param name="name"> 键名 </param>
         protected   virtual   void  RemoveSession( string  name)
        {
            
if  (Session[name]  !=   null )
            {
                Session.Remove(name);
            }
        }

        
///   <summary>
        
///  获取Session值
         
///   </summary>
        
///   <param name="name"> 键名 </param>
        
///   <returns> 键值 </returns>
         protected   virtual   object  GetFromSession( string  name)
        {
            
return  Session[name];
        }

        
///   <summary>
        
///  将对象保存到ViewState中
         
///   </summary>
        
///   <param name="name"> 键名 </param>
        
///   <param name="value"> 键值 </param>
         protected   virtual   void  SaveToViewState( string  name,  object  value)
        {
            
if  (ViewState[name]  ==   null )
            {
                ViewState.Add(name, value);
            }
            
else
            {
                ViewState[name] 
=  value;
            } 
        }

        
///   <summary>
        
///  获取ViewState值
         
///   </summary>
        
///   <param name="name"> 键名 </param>
        
///   <returns> 键值 </returns>
         protected   virtual   object  GetFromViewState( string  name)
        {
            
return  ViewState[name];
        }

        
///   <summary>
        
///  将对象从ViewState中移出
         
///   </summary>
        
///   <param name="name"> 键名 </param>
         protected   virtual   void  RemoveViewState( string  name)
        {
            
if  (ViewState[name]  !=   null )
            {
                ViewState.Remove(name);
            }
        }

        
///   <summary>
        
///  弹出提示信息
         
///   </summary>
        
///   <param name="msg"> 信息内容 </param>
         protected   virtual   void  ShowMessage( string  msg)
        {
            ClientScript.RegisterStartupScript(GetType(), 
" ___asp2TrainingShowMessage " ,
                                                 
" <script language=\ " javascript\ " >var str=\ ""  +
                                                 msg.Replace( " \\ " " \\\\ " ).Replace( " \r\n " "   " ).Replace( " \ "" "' ") +
                                                  " \ " ;alert(str.replace( /< br >/ g, ' \\n ' ).replace( /< BR >/ g, ' \\n ' )); </ script > " );
        }

        
///   <summary>
        
///  向页面写入脚本
         
///   </summary>
        
///   <param name="key"> Key </param>
        
///   <param name="content"> 内容 </param>
         protected   virtual   void  RegisterJavaScript( string  key, string  content)
        {
            
if  (key  ==   null   ||  key.Equals( "" ))
            {
                key 
=   " ___asp2TrainingFunctionJavascript " ;
            }

            
if  (content  ==   null )
            {
                content 
=   "" ;
            }

            ClientScript.RegisterStartupScript(GetType(), key, 
" <script> "   +  content  +   " </script> " );
        }

        
///   <summary>
        
///  检查用户权限
         
///   </summary>
         protected   virtual   bool  CheckPermission()
        {
            
if  (Session[ " userModuleList " !=   null )
            {
                ArrayList userModuleList 
=  (ArrayList)Session[ " userModuleList " ];
                
string  path  =  Request.Path.Substring(Request.ApplicationPath.Length  +   1 );
                
if  ( ! userModuleList.Contains(path))
                {
                    Response.Redirect(Request.ApplicationPath 
+   " /NoPermission.aspx " );
                    
return   false ;
                }
                
else
                {
                    
return   true ;
                }
            }
            
else
            {
                Response.Write(
" <script language=\ " javascript\ " >alert('您未登录或长时间未操作,请重新登录');window.top.location = ' "   +  Request.ApplicationPath  +   " ';</script> " );
                
return   false ;
            }
        }

        
protected   int  LoginUser_Id
        {
            
get  
            {
                
if  (Session[ " loginuser_id " ==   null )
                {
                    
return   0 ;
                }
                
else
                {
                    
return   int .Parse(Session[ " loginuser_id " ].ToString());
                }
            }
        }

        
protected   string  LoginUser_Address
        {
            
get  
            {
                
return  Page.Request.UserHostAddress;
            }
        }
    }
}

把一些,如页面跳转,消息提示,页面加载验证(是否登录等信息)--可以放到基类中

你可能感兴趣的:(C#)