扩展页面基类可以实现太多功能了,本篇是我平时用到的一些功能的整理。
包括:企业库操作简化,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属性来进行权限验证和数据库操作了。

二. 动态Theme(Theme选择器)
首先要override Theme和StyleSheetTheme两个属性
What we can do in "Page" class 页面基类功能扩展汇总        // **************************************
What we can do in "Page" class 页面基类功能扩展汇总        
//          Dynamic Theme 
What we can do in "Page" class 页面基类功能扩展汇总        
// **************************************
What we can do in "Page" class 页面基类功能扩展汇总
        public override string StyleSheetTheme

这里我用了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>
    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());
    }

三.动态本地化(语言选择器)
语言选择器有多种做法,其中一种就是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是的不到的。需要使用以下代码:
        protected override void InitializeCulture()
        {
            
string culture = this.Context.Profile.GetPropertyValue("Culture").ToString();
            
if (!string.IsNullOrEmpty(culture))
                
this.UICulture = CultureInfo.CreateSpecificCulture(culture).ToString();
        }
然后就是做个DropDown设定Profile,代码略


四.一些ASP.NET AJAX功能
What we can do in "Page" class 页面基类功能扩展汇总    // 注册脚本
What we can do in "Page" class 页面基类功能扩展汇总
    public bool IsInAsyncPostBack
    }

五.自定义ViewState的存贮
见:http://www.cnblogs.com/jackielin/archive/2005/11/25/284626.html

六.判断页面刷新
见:http://www.codeproject.com/aspnet/Detecting_Refresh.asp

相关文章:

  • 2021-12-03
  • 2021-08-20
  • 2021-11-11
  • 2022-12-23
  • 2022-12-23
  • 2021-07-20
  • 2021-04-22
  • 2021-09-11
猜你喜欢
  • 2022-03-03
  • 2021-08-13
  • 2021-07-17
  • 2022-12-23
  • 2021-10-01
  • 2021-09-28
  • 2021-08-06
相关资源
相似解决方案