杞人忧天上掉下个林妹妹

穿越旷野的妹妹啊,慢些走;请不要用你的沉默告诉我,你不回头!

导航

ASP.NET 继承Page

ASP.NET1.1

 public class BasePage : System.Web.UI.Page
 {
  public BasePage()
  {
  }
  //使用权限判断
  override protected void OnInit(EventArgs e)
  {
   base.OnInit(e);
   if(this.Session["UserName"] == null)
   {
    this.Response.Redirect("ErrorPage.aspx");
   }
   else
   {
    //设置权限
   }
   //获取请求的文件相对路径
   this.Response.Write(this.Request.Path);
  }
  //自定义方法
  public void WriteList(string info)
  {
   BasePage curPage = (BasePage)System.Web.HttpContext.Current.Handler;
   curPage.Response.Write("<br>获取当前页对象进行操作<br>");
   this.Response.Write(info + "<br>");
  }
  //对所有页面的错误处理
  override protected void OnError(System.EventArgs e)
  {
     this.Response.Redirect("Error.aspx?ErrorMess=" + Server.UrlPathEncode(Server.GetLastError().Message));   //base.OnError(e);  
  }
   //自定义属性
   public bool IsAdmin
  {
   get
   {
    return(true);
   }
  }
   //自定义属性
  public bool IsCommonlyUser
  {
   get
   {
    return(true);
   }
  }
 }



[转贴一]  重写Page类

using System;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Security.Cryptography;

namespace Web.Components
{

 public class Page : System.Web.UI.Page
 {

  protected Config cfg = new Config();

  override protected void OnInit(EventArgs e)
  {

   Page_Init();

   if( IsPostBack )
   {

    this.Load += new System.EventHandler( this.PostBack_Load );

    base.OnInit(e);
   }

   Response.Write( cfg.DEFAULT_DOCUMENT_TYPE );

   this.Load += new System.EventHandler(this.Page_Load);

   base.OnInit(e);
  }

  protected bool IsUser
  {
 
   get
   {

    Database db = new Database( "select * from sys_users where username=@username and password=@password" );

    db["@username",SqlDbType.VarChar] = UserName;
    db["@password",SqlDbType.VarChar] = Password;

    int count = db.Count;

    while( db.Read() )
    {
   
     UserID = int.Parse( db[0].ToString() );
    }

    db.Close();

    return ( count>0 );
   }
  }

  protected string UserName
  {
 
   get
   {
  
    return ( Request.Cookies["UserName"]==null )?"":Server.HtmlEncode( Request.Cookies["UserName"].Value );
   }
   set
   {

    //Response.Cookies["UserName"].Expires = DateTime.Now.AddSeconds( 0 );
    //Response.Cookies["UserName"].Expires = DateTime.Now.AddDays( 1 );
    Response.Cookies["UserName"].Value = value;
   }
  }

  protected string Password
  {
 
   get
   {
  
    return ( Request.Cookies["Password"]==null )?"":Server.HtmlEncode( Request.Cookies["Password"].Value );
   }
   set
   {
  
    //Response.Cookies["Password"].Expires = DateTime.Now.AddSeconds( 0 );
    //Response.Cookies["Password"].Expires = DateTime.Now.AddDays( 1 );
    Response.Cookies["Password"].Value = GetEncodePassword( value );
   }
  }

  protected int UserID
  {
 
   get
   {
  
    return ( Session["UserId"]==null )?0:int.Parse( Session["UserId"].ToString() );
   }
   set
   {
  
    Session["UserId"] = value;
   }
  }

  protected void WriteScript( string str )
  {
 
   str = GetString( "<script language=javascript>try{ {0}; }catch(ex){}</script>\n",str );

   Response.Write( str );
  }

  protected string GetFileString(string path,params string[] args)
  {
  
   path = Request.PhysicalApplicationPath + "/" + path;

   StreamReader reader = new StreamReader(GetString(path,args),Encoding.Default);

   string value = string.Empty;

   try
   {
    value = reader.ReadToEnd();
   }
   catch(IOException ex)
   {
    value = ex.Message;
   }
   finally
   {
    reader.Close();
   }

   return value;
  }
  public string GetString(string value,params object[] args)
  {

   for(int i=0;i<args.Length;i++)
   {

    value = value.Replace("{"+i.ToString()+"}",args[i].ToString());
   }
  
   return value;
  }

  protected string GetLocalFileName()
  {
 
   return Request.FilePath.Split('/')[Request.FilePath.Split('/').Length-1].ToLower();
  }

  protected string GetUrlDecode( string str )
  {
 
   return Server.UrlDecode( str );
  }
  //将字符串进行编码
  protected string GetEncodeString(string value,int count)
  {
  
   return Convert.ToBase64String(Encoding.Default.GetBytes(value));;
  }
  //将已编码的字符串进行解码
  protected string GetDecodeString(string value,int count)
  {

   return Encoding.Default.GetString(Convert.FromBase64String(value));;
  }
  //MD5加密字符串
  protected string GetEncodePassword(string value)
  {
    
   return value;
  }
  //获得一个值,该值指示该页是否正为响应客户端回发而加载,或者它是否正被首次访问
  protected new bool IsPostBack
  {
 
   get{ return ( Request.Form.Count > 0 ) || ( Request.Files.Count > 0 ) || ( Request.ServerVariables["REQUEST_METHOD"] == "POST" ) ; }
  }
  //页面加载
  protected virtual void Page_Init() { }
  //首次访问加载
  protected virtual void Page_Load( object sender, System.EventArgs e ) { }
  //响应客户端回发而加载
  protected virtual void PostBack_Load( object sender, System.EventArgs e ){  }
 }
}

[转贴二] ASP.Net 2.0 中 UserControl和Page的继承

1.动态装载UserControl
在1.x里,动态装载UserControl是很简单的,
例如:MyControl myControl = (MyControl)LoadControl("~/MyControl.ascx");
而在2.0里,却还需要在页面里加上<%@ Reference Control="~/MyControl.ascx" %>
这里就有了一个问题,如果我要装载的UserControl是不可预知的,那我必需把在页面添加所有的可能的UserControl引用,麻烦。
再如果我可能在运行时生成UserControl,那么装载这样的UserControl,那就还必需在动态修改页面的引用

2.UserControl继承
在1.x里,例如有 UserControl基类
public class BaseUserControl : System.Web.UI.UserControl{...}
继承:
public class DerivedControl :BaseUserControl{...}
BaseUserControl只需要在一个CS文件定义就可以(BaseUserControl只是一个普通的类)
在2.0中,则BaseUserControl必需定义在一个UserControl里面,就是BaseUserControl必需是一个完整的UserControl(包括*.ascx和*.ascx.cs或*.ascx.vb)
假如已经定义一个完整的BaseUserControl
继承:
在DerivedControl.ascx.cs中
public partial class DerivedControl : BaseUserControl{}
在DerivedControl.ascx中必需添加 <%@ Reference Control="~/BaseUserControl.ascx" %>
而且它还必需在<mailto:%@Control> 的前面
<%@ Control Language="C#" AutoEventWireup="true" CodeFileBaseClass="BaseUserControl"
CodeFile="~/DerivedControl.ascx.cs" Inherits="DerivedControl" %>
CodeFileBaseClass指定父类类型

3.Page继承
与UserControl继承类似,需要从一个完整Page×(包括*.aspx和*.aspx.cs)来继承
在1.x里,例如有 Page基类
public class BasePage : System.Web.UI.Page{...}
在DerivedPage.aspx.cs中
public partial class DerivedPage : BasePage{}
在DerivedPage.aspx中必需添加 <%@ Reference Control="~/BasePage.aspx" %>
它也必需在<mailto:%@Page> 的前面。
<%@ Control Language="C#" AutoEventWireup="true" CodeFileBaseClass="BasePage"
CodeFile="~/DerivedPage.ascx.cs" Inherits="DerivedPage" %>

要指出的是DerivedPage.aspx存根代码(即ASPNet2.0由*.aspx自动生成的那部分)会隐藏BasePage.aspx的存根代码
(UserControl也一样)例如在BasePage.aspx中定义一个控件如Label1,而在DerivedPage.aspx没有定义它,而在DerivedPage中加入了操作Label1的代码,在预编译的时候,都是正常的,但在运行时访问DerivedPage.aspx就会报错,指出对Label1引用没有指定到一个对象的实例。那么我们可以简单地认为,在运行时,ASP.Net只会对所请求的页面的控件进行实例化,当然我们也可以在后台手工进行控件实例化。当然我们访问BasePage.aspx是不会有错的。

posted on 2007-04-28 23:48  杞人  阅读(2819)  评论(2编辑  收藏  举报