时间:2021-05-20
本文实例讲述了C# Page用于各页面继承功能的实现方法。分享给大家供大家参考。具体方法如下:
IBasePage.cs文件如下:
复制代码 代码如下:/// <summary>
/// 用于页面或用户控件
/// </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>
/// 整站页面基础类
/// </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#程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例分析了C#接口(Interface)用法。分享给大家供大家参考。具体分析如下:继承"基类"跟继承"接口"都能实现某些相同的功能,但有些接口能够完成的功能
本文实例讲述了C#简单输出日历的方法。分享给大家供大家参考。具体如下:用C#输出日历,此功能可用于Ajax方式列出计划日程相关的内容,由于是C#控制输出,可以方
本文实例讲解了C#的文件管理类,帮助大家对类的大体功能做简单的了解。该实例应用于C#项目的时候判断文件是否存在、创建文件、删除文件等情况;今天大体总结文件调取功
本文实例讲述了C#获取哈希加密生成随机安全码的类。分享给大家供大家参考。具体分析如下:这个C#类封装了一些hash加密的功能,可以用于得到随机哈希加密字符串使用
最近学习了C#实现文件上传与下载,现在分享给大家。1、C#文件上传创建MyUpload.htm页面,用于测试创建UploadFile.aspx文件,在Uploa