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 ){ } } }
[转贴二] 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是不会有错的。
|
请发表评论