扩展页面基类可以实现太多功能了,本篇是我平时用到的一些功能的整理。
包括:企业库操作简化,Theme选择器,语言选择器,AJAX,ViewState存储等。
一.简化Enterprise Library操作
这里举Data Access Application Block和Security Application Block两个例子。
在你的页面基类里(我这里命名为BasePage)加入以下代码:
二. 动态Theme(Theme选择器)
首先要override Theme和StyleSheetTheme两个属性
// **************************************
// Dynamic Theme
// **************************************
public override string StyleSheetTheme
{
get
{
return (Request.Cookies["PreferredTheme"] != null) ? Request.Cookies["PreferredTheme"].Value : base.StyleSheetTheme;
}
set
{
base.StyleSheetTheme = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public override string Theme
{
get
{
return (Request.Cookies["PreferredTheme"] != null) ? Request.Cookies["PreferredTheme"].Value : base.Theme;
}
set
{
base.Theme = value;
}
}
这里我用了cookie当然你也可以存到profile里
然后做一个Theme选择器,我这里用的是RadioButtonList,你也可以用DropDown之类的
三.动态本地化(语言选择器)
语言选择器有多种做法,其中一种就是override Page类的InitializeCulture
四.一些ASP.NET AJAX功能
// 注册脚本
public bool IsInAsyncPostBack
{
get
{
ScriptManager manage = ScriptManager.GetCurrent(this);
if (manage != null)
{
return manage.IsInAsyncPostBack;
}
return false;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public void RegisterStartupScript(Control control, string key, string script)
{
Type type = control.GetType();
if (IsInAsyncPostBack)
{
ScriptManager.RegisterStartupScript(control, type, key, script, true);
}
else
{
if (!ClientScript.IsStartupScriptRegistered(type, key))
ClientScript.RegisterStartupScript(type, key, script, true);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//根据profile控制局部刷新
public bool EnablePartialRender
{
get
{
ProfileCommon p = (ProfileCommon)this.Context.Profile;
return p.EnablePartialRender;
}
set
{
ProfileCommon p = (ProfileCommon)this.Context.Profile;
p.EnablePartialRender = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
private void SetPartialRender()
{
if (!EnablePartialRender)
{
ScriptManager manager = ScriptManager.GetCurrent(this);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (manager != null && manager.EnablePartialRendering)
{
manager.EnablePartialRendering = false;
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
protected override void OnPreInit(EventArgs e)
{
SetPartialRender();
base.OnPreInit(e);
}
五.自定义ViewState的存贮
见:http://www.cnblogs.com/jackielin/archive/2005/11/25/284626.html
六.判断页面刷新
见:http://www.codeproject.com/aspnet/Detecting_Refresh.asp
包括:企业库操作简化,Theme选择器,语言选择器,AJAX,ViewState存储等。
一.简化Enterprise Library操作
这里举Data Access Application Block和Security Application Block两个例子。
在你的页面基类里(我这里命名为BasePage)加入以下代码:
private static Database _db;
private static IAuthorizationProvider _ruleProvider;
static BasePage()
{
_db = DatabaseFactory.CreateDatabase();
_ruleProvider = AuthorizationFactory.GetAuthorizationProvider("RuleProvider");
}
public IAuthorizationProvider RuleProvider { get { return _ruleProvider; } }
public Database DB { get { return _db; } }
protected bool Authorize(string context)
{
return RuleProvider.Authorize(this.User, context);
}
这样就可以直接用Authorize方法和DB属性来进行权限验证和数据库操作了。private static IAuthorizationProvider _ruleProvider;
static BasePage()
{
_db = DatabaseFactory.CreateDatabase();
_ruleProvider = AuthorizationFactory.GetAuthorizationProvider("RuleProvider");
}
public IAuthorizationProvider RuleProvider { get { return _ruleProvider; } }
public Database DB { get { return _db; } }
protected bool Authorize(string context)
{
return RuleProvider.Authorize(this.User, context);
}
二. 动态Theme(Theme选择器)
首先要override Theme和StyleSheetTheme两个属性
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
这里我用了cookie当然你也可以存到profile里
然后做一个Theme选择器,我这里用的是RadioButtonList,你也可以用DropDown之类的
<asp:RadioButtonList runat="server" ID="ThemeChooser" AutoPostBack="true" OnSelectedIndexChanged="ThemeChanged">
<asp:ListItem Text="Enhanced" Value="1" />
<asp:ListItem Text="Basic" Value="0" />
<asp:ListItem Text="None" Value="-1" />
</asp:RadioButtonList>
<asp:ListItem Text="Enhanced" Value="1" />
<asp:ListItem Text="Basic" Value="0" />
<asp:ListItem Text="None" Value="-1" />
</asp:RadioButtonList>
protected void ThemeChanged(object sender, EventArgs e)
{
HttpCookie cookie = new HttpCookie("PreferredTheme");
cookie.Value = ThemeChooser.SelectedItem.Text;
if (Response.Cookies["PreferredTheme"] == null)
{
Response.Cookies.Add(cookie);
}
else
{
Response.Cookies.Set(cookie);
}
Response.Redirect(Request.Url.ToString());
}
{
HttpCookie cookie = new HttpCookie("PreferredTheme");
cookie.Value = ThemeChooser.SelectedItem.Text;
if (Response.Cookies["PreferredTheme"] == null)
{
Response.Cookies.Add(cookie);
}
else
{
Response.Cookies.Set(cookie);
}
Response.Redirect(Request.Url.ToString());
}
三.动态本地化(语言选择器)
语言选择器有多种做法,其中一种就是override Page类的InitializeCulture
//*************************************
// For Localization
//*************************************
protected override void InitializeCulture()
{
ProfileCommon p = (ProfileCommon)this.Context.Profile;
if (!String.IsNullOrEmpty(p.Culture))
this.UICulture = CultureInfo.CreateSpecificCulture(p.Culture).Name;
}
这里我把语言设定以sting形式存到了Culture这个Profile里,但如果你把Page扩展类放在一个单独的class libery里定义的话,强类型的ProfileCommon是的不到的。需要使用以下代码:// For Localization
//*************************************
protected override void InitializeCulture()
{
ProfileCommon p = (ProfileCommon)this.Context.Profile;
if (!String.IsNullOrEmpty(p.Culture))
this.UICulture = CultureInfo.CreateSpecificCulture(p.Culture).Name;
}
protected override void InitializeCulture()
{
string culture = this.Context.Profile.GetPropertyValue("Culture").ToString();
if (!string.IsNullOrEmpty(culture))
this.UICulture = CultureInfo.CreateSpecificCulture(culture).ToString();
}
然后就是做个DropDown设定Profile,代码略{
string culture = this.Context.Profile.GetPropertyValue("Culture").ToString();
if (!string.IsNullOrEmpty(culture))
this.UICulture = CultureInfo.CreateSpecificCulture(culture).ToString();
}
四.一些ASP.NET AJAX功能
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
五.自定义ViewState的存贮
见:http://www.cnblogs.com/jackielin/archive/2005/11/25/284626.html
六.判断页面刷新
见:http://www.codeproject.com/aspnet/Detecting_Refresh.asp