C# Page用于各页面继承功能实例
本文实例讲述了c# page用于各页面继承功能的实现方法。分享给大家供大家参考。具体方法如下:
ibasepage.cs文件如下:
/// 用于页面或用户控件
/// </summary>
public interface ibasepage
{
/// <summary>
/// 数据缓存类
/// </summary>
dbcache cachehelp { get; }
/// <summary>
/// 页面视图,用于页面级别访问,模仿mvc
/// </summary>
viewdatadict viewdata { get; }
/// <summary>
/// 获取datatable.rows,此方法不会出错异常
/// </summary>
/// <param name="pname">数据名称</param>
/// <returns></returns>
datarowcollection viewdatarows(string pname);
/// <summary>
/// 1:true否则false
/// </summary>
/// <param name="pname"></param>
/// <returns></returns>
bool isviewdata(string pname);
/// <summary>
/// url参数类
/// </summary>
urlquery uq { get; }
/// <summary>
/// 页面搜索关键词,搜索关键词是最基本功能,故在这里放一个字段
/// </summary>
string searchstr { get; set; }
/// <summary>
/// 权限使用次数
/// </summary>
int userfuncount { get; set; }
/// <summary>
/// 数据库链接次数
/// </summary>
int dbcount { get; set; }
/// <summary>
/// 用与判断增加style,js等是否已重复添加
/// </summary>
/// <param name="pname"></param>
/// <returns></returns>
templisthandler templist { get; }
}
basepage.cs文件如下:
/// 整站页面基础类
/// </summary>
public class basepage : system.web.ui.page, ibasepage
{
private static readonly datarowcollection emptyrows = new datatable().rows;
private dbcache cachehelp;
private viewdatadict viewdata;
private viewdatadict viewdatam;
private int userfuncount = 0;//权限使用次数
private int dbcount = 0;//数据库连接次数
private templisthandler templist;
protected bool isopenxingnengtongji = true;
#region 搜索关键词
//private string _searchstr;
public string searchstr
{
get
{
//if (this._searchstr == null)
//{
// return string.empty;
//}
return viewdata["keyword"].tostring();
}
set
{
//this._searchstr = value;
viewdata["keyword"] = value;
}
}
#endregion
#region 将数据保存在页面控件里 viewdatapage
/// <summary>
/// 将数据保存在页面控件里
/// </summary>
public string viewdatapage
{
get
{
return req.form("__viewdatapage");
}
set
{
this.clientscript.registerhiddenfield("__viewdatapage", value);
//this.clientscript.registerclientscriptblock(this.gettype(), "__urlreferrer", "<input id=\"__urlreferrer\" name=\"__urlreferrer\" type=\"hidden\" value=\"" + value + "\" />");
}
}
#endregion
public basepage()
{
cachehelp = new dbcache();
viewdata = new viewdatadict();
viewdatam = new viewdatadict("model_");
templist = new templisthandler();
}
private datetime __time1;
private datetime __time2;
private static int __i = 0;
#region 跟根url重写获取参数
/// <summary>
/// 跟根url重写获取参数
/// </summary>
private urlquery _uq;
protected override void oninit(eventargs e)
{
_uq = new urlquery(true);
__time1 = datetime.now;
base.oninit(e);
}
protected override void onpreinit(eventargs e)
{
//"onpreinit".writeline();
base.onpreinit(e);
}
#endregion
#region 基本信息统计
protected override void render(system.web.ui.htmltextwriter writer)
{
//"render".writeline();
base.render(writer);
__i++;
__time2 = datetime.now;
timespan ts = __time2 - __time1;
if (isopenxingnengtongji)
response.write(@"<div style=""text-align:center;color:#ccc"">number:" + __i + ", time:" + ts.totalmilliseconds + "ms, db=" + this.dbcount + ",fun=" + this.userfuncount + "</div>");
}
#endregion
protected override void onunload(eventargs e)
{
//"onunload".writeline();
base.onunload(e);
}
#region 将搜索到的结果为醒目显示
/// <summary>
/// 将搜索到的结果为醒目显示
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public string replacesearchtext(object str)
{
if (searchstr == null || searchstr.length == 0)
return str.tostring();
//return str.tostring().replace(searchstr, "<strong style=\"color:#f00\">" + searchstr + "</strong>");
return regex.replace(str.tostring(), regex.escape(searchstr), "<strong style=\"color:#f00\">" + searchstr + "</strong>", regexoptions.ignorecase);
}
#endregion
#region ibasepage 成员
/// <summary>
/// 数据缓存类
/// </summary>
public dbcache cachehelp
{
get { return this.cachehelp; }
}
/// <summary>
/// 页面视图,用于页面级别访问,模仿mvc
/// </summary>
public viewdatadict viewdata
{
get { return this.viewdata; }
}
/// <summary>
/// 页面视图(model),用于页面级别访问,模仿mvc
/// </summary>
public viewdatadict viewdatam
{
get { return this.viewdatam; }
}
/// <summary>
/// 获取datatable.rows,此方法不会出错异常
/// </summary>
/// <param name="pname"></param>
/// <returns></returns>
public datarowcollection viewdatarows(string pname)
{
datatable dt = viewdata[pname] as datatable;
if (dt == null)
{
return emptyrows;
}
return dt.rows;
}
public bool isviewdata(string pname)
{
return viewdata[pname].tostring() == "1";
}
public urlquery uq
{
get { return this._uq; }
}
///// <summary>
///// 获取系统配置
///// </summary>
///// <param name="key"></param>
///// <returns></returns>
//public string sysconfig(string key)
//{
// return wconfigbll.getvalue(key).tostring();
//}
public templisthandler templist
{
get { return this.templist; }
}
public int userfuncount
{
get { return this.userfuncount; }
set { this.userfuncount = value; }
}
public int dbcount
{
get { return this.dbcount; }
set { this.dbcount = value; }
}
#endregion
#region itrace 成员
//private int dbconnectioncount;
///// <summary>
///// 数据库连接次数
///// </summary>
//public int dbconnectioncount
//{
// get
// {
// return this.dbconnectioncount;
// }
// set
// {
// dbconnectioncount = value;
// }
//}
#endregion
#region 获取或设置提交后返回的路径,若未指定,则是当前url
/// <summary>
/// 获取或设置提交后返回的路径,若未指定,则是当前url
/// </summary>
public string urlreferrer
{
get
{
string s = req.form("___urlreferrer");
if (s.length == 0)
{
s = viewdata["___urlreferrer"].tostring();
}
if (s.length == 0)
{
s = request.url.tostring();
}
return s;
}
set
{
viewdata["___urlreferrer"] = value;
this.clientscript.registerhiddenfield("___urlreferrer", value);\" type=\"hidden\" value=\"" + value + "\" />");
}
}
#endregion
}
希望本文所述对大家的c#程序设计有所帮助。