惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Google DeepMind News
Google DeepMind News
aimingoo的专栏
aimingoo的专栏
Microsoft Security Blog
Microsoft Security Blog
T
Tenable Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
W
WeLiveSecurity
D
DataBreaches.Net
Attack and Defense Labs
Attack and Defense Labs
H
Heimdal Security Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
AI
AI
P
Proofpoint News Feed
PCI Perspectives
PCI Perspectives
Schneier on Security
Schneier on Security
T
Threatpost
GbyAI
GbyAI
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
H
Help Net Security
F
Full Disclosure
T
Threat Research - Cisco Blogs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
M
MIT News - Artificial intelligence
L
Lohrmann on Cybersecurity
Martin Fowler
Martin Fowler
博客园 - 【当耐特】
Y
Y Combinator Blog
腾讯CDC
The Hacker News
The Hacker News
博客园 - Franky
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园_首页
Simon Willison's Weblog
Simon Willison's Weblog
L
LINUX DO - 最新话题
Security Latest
Security Latest
Know Your Adversary
Know Your Adversary
Forbes - Security
Forbes - Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
SegmentFault 最新的问题
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
L
LangChain Blog
Vercel News
Vercel News
Cisco Talos Blog
Cisco Talos Blog
量子位
P
Proofpoint News Feed
H
Hacker News: Front Page
Help Net Security
Help Net Security
L
LINUX DO - 热门话题
Project Zero
Project Zero
C
Cisco Blogs

博客园 - J. Lin

据可靠小道消息VS 2008 SP1 RTM 将在下周一发布 Configuration Section Designer 自定义配置设计器 Linq to Sql: 批量删除之投机取巧版 [CSS Hack] border-color:transparent & filter+ClearType bug ASP.NET AJAX 1.0 & AJAX Control Toolkit 在iframe中的"access denied"错误 Aptana使用入门一:Code Assist 不可多得的Javascript(AJAX)开发工具 - Aptana 右键菜单快速打开VS 2005 Website项目 SQL Server 2005 SP1 安装问题 设置asp.net程序在web.config被修改后是否重启 VS2005 Add-in:CSS Properties Window 在MastPage中引用脚本资源 Design Templates for ASP.NET 2.0 Security Guidelines: ASP.NET 2.0 [目录] & [How to列表] ASP.NET 2.0 Security FAQs 翻译计划 [ASP.NET 2.0 Security FAQs]如何在membership中强制使用高安全性的密码 [ASP.NET 2.0 Security FAQs]如何设置SQL Server或SQL Express数据库,使其支持Membership、Profiles和Role Web.config 文件中的“智能感知” 用宏自动生成Web.sitemap文件(ASP.NET 2.0)
What we can do in "Page" class 页面基类功能扩展汇总
J. Lin · 2006-11-06 · via 博客园 - J. Lin

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

        // **************************************
        
//          Dynamic Theme 
        
// **************************************
        public override string StyleSheetTheme
        
{
            
get
            
{
                
return (Request.Cookies["PreferredTheme"!= null? Request.Cookies["PreferredTheme"].Value : base.StyleSheetTheme;
            }

            
set
            
{
                
base.StyleSheetTheme = value;
            }

        }


        
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之类的

<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功能

    // 注册脚本
    public bool IsInAsyncPostBack
    
{
        
get
        
{
            ScriptManager manage 
= ScriptManager.GetCurrent(this);
            
if (manage != null)
            
{
                
return manage.IsInAsyncPostBack;
            }

            
return false;
        }

    }


    
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);
        }

    }


    
//根据profile控制局部刷新
    public bool EnablePartialRender
    
{
        
get
        
{
            ProfileCommon p 
= (ProfileCommon)this.Context.Profile;
            
return p.EnablePartialRender;
        }

        
set
        
{
            ProfileCommon p 
= (ProfileCommon)this.Context.Profile;
            p.EnablePartialRender 
= value;
        }

    }


    
private void SetPartialRender()
    
{
        
if (!EnablePartialRender)
        
{
            ScriptManager manager 
= ScriptManager.GetCurrent(this);

            
if (manager != null && manager.EnablePartialRendering)
            
{
                manager.EnablePartialRendering 
= false;
            }

        }

    }


    
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