简介
我们都知道Asp.Net2.0中aspx文件结构相比以前有了很大的改变。当然,其中大多数是改善,比如,在page的source code界面的内嵌<% ... %>代码支持智能提示了,不过这个智能提示也有小小小的不爽,就是除非是System.Web等几个默认的命名空间中的类,访问其他命名空间的类如果不添加Namespace引用的话就必须用完全路径访问(我的类前面的命名空间前缀会很长啊,很难看的);有一些改善也带来一些限制,比如,除非在页面/用户控件中显式import其他页面/用户控件,否则是不能访问到外部类型的,感觉就好像这个页面/控件自己一个人带一个程序集。为了合理避免一些限制造成的语法的不爽和合理利用新的智能功能,Teddy在本文中向您展示一组MasterPage/Page/UserControl通用基类封装,他们将为您提供许多非常方便的功能和语法体验。当然,唯一的代价是,你的Page/MasterPage/UserControl必须从我的基类继承,当然,从我的基类继承的对象和从默认系统基类继承的对象是可以自由混合使用的,付出这一点点代价,您在下文中将会看到,绝对是值得的。
正文因为,MasterPage和UserControl的基类提供的功能和Page提供的功能在接口上是完全一致的,我这里就只介绍Ilungasoft.Helper.Web.UI.Page这个类了。
Page类的源码
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Resources;
namespace Ilungasoft.Helper.Web.UI
{
public class Page : System.Web.UI.Page
{
Helper Methdos#region Helper Methdos
/**//// <summary>
/// Gets the string param.
/// </summary>
/// <param name="paramName">Name of the param.</param>
/// <param name="errorReturn">The error return.</param>
/// <returns></returns>
protected string GetStringParam(string paramName, string errorReturn)
{
return WebHelper.GetStringParam(Request, paramName, errorReturn);
}
/**//// <summary>
/// Gets the int param.
/// </summary>
/// <param name="paramName">Name of the param.</param>
/// <param name="errorReturn">The error return.</param>
/// <returns></returns>
protected int GetIntParam(string paramName, int errorReturn)
{
return WebHelper.GetIntParam(Request, paramName, errorReturn);
}
/**//// <summary>
/// Gets the date time param.
/// </summary>
/// <param name="paramName">Name of the param.</param>
/// <param name="errorReturn">The error return.</param>
/// <returns></returns>
protected DateTime GetDateTimeParam(string paramName, DateTime errorReturn)
{
return WebHelper.GetDateTimeParam(Request, paramName, errorReturn);
}
/**//// <summary>
/// Strongs the typed.
/// </summary>
/// <param name="obj">The obj.</param>
/// <returns></returns>
protected static ObjectType StrongTyped<ObjectType>(object obj)
{
return WebHelper.StrongTyped<ObjectType>(obj);
}
/**//// <summary>
/// Toes the js single quote safe string.
/// </summary>
/// <param name="str">The STR.</param>
/// <returns></returns>
protected static string ToJsSingleQuoteSafeString(string str)
{
return WebHelper.ToJsSingleQuoteSafeString(str);
}
/**//// <summary>
/// Toes the js double quote safe string.
/// </summary>
/// <param name="str">The STR.</param>
/// <returns></returns>
protected static string ToJsDoubleQuoteSafeString(string str)
{
return WebHelper.ToJsDoubleQuoteSafeString(str);
}
/**//// <summary>
/// Toes the VBS quote safe string.
/// </summary>
/// <param name="str">The STR.</param>
/// <returns></returns>
protected static string ToVbsQuoteSafeString(string str)
{
return WebHelper.ToVbsQuoteSafeString(str);
}
/**//// <summary>
/// Toes the SQL quote safe string.
/// </summary>
/// <param name="str">The STR.</param>
/// <returns></returns>
protected static string ToSqlQuoteSafeString(string str)
{
return WebHelper.ToSqlQuoteSafeString(str);
}
/**//// <summary>
/// Texts to HTML.
/// </summary>
/// <param name="txtStr">The TXT STR.</param>
/// <returns></returns>
protected static string TextToHtml(string txtStr)
{
return WebHelper.TextToHtml(txtStr);
}
#endregion
CryptographyHelper#region CryptographyHelper
protected WebHelper.CryptographyHelper Cryptography = new WebHelper.CryptographyHelper();
#endregion
ClientScriptFactoryHelper#region ClientScriptFactoryHelper
protected sealed class PageClientScriptFactory : WebHelper.ClientScriptFactoryHelper
{
/**//// <summary>
/// Initializes a new instance of the <see cref="T:PageClientScriptFactory"/> class.
/// </summary>
public PageClientScriptFactory()
: base()
{
}
/**//// <summary>
/// Calls the client validator.
/// </summary>
/// <param name="holder">The holder.</param>
/// <param name="validators">The validators.</param>
/// <returns></returns>
public new string CallClientValidator(string contentPlaceHolder, params System.Web.UI.WebControls.BaseValidator[] validators)
{
return base.CallClientValidator("ctl00_" + contentPlaceHolder + "_", validators);
}
/**//// <summary>
/// Calls the client validator.
/// </summary>
/// <param name="validators">The validators.</param>
/// <returns></returns>
public string CallClientValidator(params System.Web.UI.WebControls.BaseValidator[] validators)
{
return base.CallClientValidator(null, validators);
}
}
protected PageClientScriptFactory ClientScriptFactory = new PageClientScriptFactory();
/**//// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.Init"></see> event to initialize the page.
/// </summary>
/// <param name="e">An <see cref="T:System.EventArgs"></see> that contains the event data.</param>
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
}
#endregion
Resource#region Resource
/**//// <summary>
/// Inits the resource manager.
/// </summary>
/// <param name="resourceName">Name of the resource.</param>
protected static void InitResourceManager(string resourceName)
{
WebHelper.InitResourceManager(resourceName);
}
/**//// <summary>
/// Clears the resource manager.
/// </summary>
protected static void ClearResourceManager()
{
WebHelper.ClearResourceManager();
}
/**//// <summary>
/// Gets the string.
/// </summary>
/// <param name="key">The key.</param>
/// <returns></returns>
protected static string GetString(string key)
{
return WebHelper.GetString(key);
}
/**//// <summary>
/// Gets the string.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="ci">The ci.</param>
/// <returns></returns>
protected static string GetString(string key, System.Globalization.CultureInfo ci)
{
return WebHelper.GetString(key, ci);
}
#endregion
}
} 基类Page提供了以下几块功能:
1)Helper Methods - 最常用的Helper函数
protected string GetStringParam(string paramName, string errorReturn) //从Request.Form/Request.QueryString中获取指定类型的参数,如果Form中取到key值则忽略QueryString
protected int GetIntParam(string paramName, int errorReturn) //同上
protected DateTime GetDateTimeParam(string paramName, DateTime errorReturn) //同上
protected static ObjectType StrongTyped<ObjectType>(object obj) //强类型绑定代理,用于强类型的访问绑定数据,如在一个Repeater对象中,可以使用<%# StrongTyped<Person>(Container.DataItem).Name %>这样的强类型语法来获取绑定数据
protected static string ToJsSingleQuoteSafeString(string str) //返回可在服务端生成,安全放到客户端单引号的js字符串中的字符串
protected static string ToJsDoubleQuoteSafeString(string str) //同上,双引号安全字符串
protected static string ToVbsQuoteSafeString(string str) //同上,for vbscript
protected static string ToSqlQuoteSafeString(string str) //同上,for SQL 字符串
protected static string TextToHtml(string txtStr) //将平面文本转换为等价HTML的一个轻量级实现
2)CryptographyHelper - 常用字符串加密算法public string SymmetricEncrpyt(string str, SymmetricAlgorithm mobjCryptoService) //对称加密,第二个参数可以传递任意实现SymmetricAlgorithm基类的对称算法
public string SymmetricDecrpyt(string str, SymmetricAlgorithm mobjCryptoService) //对称解密
public string ComputeHash(string str) //计算Hash码,返回Base64字符串
3)ClientScriptFactoryHelper - 客户端Script工厂,提供常用客户端js脚本的服务端生成public string WrapScriptTag(params string[] scripts) //用一对<script language="javascript" type="text/javascript"> ... </script>包裹指定的脚本
public string PopAlert(string msg)
public string PopConfirm(string msg)
public string PopPrompt(string msg, string defaultValue)
public string CloseSelf()
public string CloseParent()
public string CloseOpener()
public string RefreshSelf()
public string RefreshOpener()
public string RefreshParent()
public string ShowModalDialog...
public string ShowModelessDialog...
public string SelfGoBack()
public string ParentGoBack()
public string OpenerGoBack()
public string Open...
protected string CallClientValidator(params System.Web.UI.WebControls.BaseValidator[] validators) //最后这个比较有意思,可以生成调用Asp.Net2.0标准验证控件的客户端脚本,例如,如果我的页面上有一个TextBox1,TextBox1有多个和它关联的验证对象,我在Page_Load中加上一句TextBox1.Attributes.Add("onblur", ClientScriptFactory.CallClientValidator(v1, v2)),其中v1,v2为验证对象的引用。那么,运行时,当用户输入完TextBox1,失去焦点时,将自动调用客户端输入验证,从而实现输入检测的及时客户端提示。是不是经常看到很多网站很酷的类似的及时验证功能呢?其实就这么简单。注意,因为当我们使用或不使用MasterPage时,运行时生成的服务端Html元素,尤其是定义在UserControl中的服务端元素的运行时ID命名规则是不一样的,这里已经为混合使用MasterPage/Page/UserControl的各种情况提供了各种重载版本。
4)Resource - 用户读取字符串资源文件的辅助函数,方便开发多语言程序
public static void InitResourceManager(string resourceName) //初始化资源管理器,参数为资源文件的名称,就是资源文件名中,国家代码前的部分
public static void ClearResourceManager()
public static string GetString(string key) //注意这里为基类Page直接定义一个GetString是很有好处的,这样在页面中可以这样调用,完全不需任何前缀:<%= GetString("HelloWorld") %>,返回指定字符串资源,这里取Thread.CurrentUICulture作为默认的资源本地化信息
public static string GetString(string key, System.Globalization.CultureInfo ci) //GetString的多语言版本
下载
Release编译版本程序集Ilungasoft.Helper.Web.dll下载 - 该下载文件可以无限制任意使用,本人不提供源码,但是我也不介意您反编译,应该很容易使用,如果,使用有问题的朋友比较多的话,我以后再提供一些使用范例,不过我相信大家都是高手一看就明白:)