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

推荐订阅源

博客园 - Franky
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Schneier on Security
Know Your Adversary
Know Your Adversary
Security Latest
Security Latest
Spread Privacy
Spread Privacy
Project Zero
Project Zero
T
The Exploit Database - CXSecurity.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
AI
AI
N
News | PayPal Newsroom
A
Arctic Wolf
NISL@THU
NISL@THU
W
WeLiveSecurity
Security Archives - TechRepublic
Security Archives - TechRepublic
Hacker News: Ask HN
Hacker News: Ask HN
P
Palo Alto Networks Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
大猫的无限游戏
大猫的无限游戏
L
Lohrmann on Cybersecurity
Last Week in AI
Last Week in AI
T
Threatpost
The Last Watchdog
The Last Watchdog
博客园_首页
C
Cybersecurity and Infrastructure Security Agency CISA
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
Engineering at Meta
Engineering at Meta
爱范儿
爱范儿
aimingoo的专栏
aimingoo的专栏
S
Security Affairs
P
Privacy & Cybersecurity Law Blog
B
Blog RSS Feed
AWS News Blog
AWS News Blog
P
Proofpoint News Feed
雷峰网
雷峰网
T
Tenable Blog
Schneier on Security
Schneier on Security
H
Heimdal Security Blog
V2EX - 技术
V2EX - 技术
V
V2EX
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Secure Thoughts
Latest news
Latest news
Help Net Security
Help Net Security
Jina AI
Jina AI
Stack Overflow Blog
Stack Overflow Blog
The Cloudflare Blog
V
Vulnerabilities – Threatpost
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org

博客园 - Edison Zhu

zhuanzai: AJAX: How to create a "Processing" modal window using UpdateProgress and ModalPopup ASP.net AJAX controls - Edison Zhu 转:Manage Web.config from XML File by using configSource attribute ASP.Net 2.0 [转载] 微软SQL Server事务隔离级别实例简介 [转载] Moving table to a different filegroup in SQL 2005 [转载] SQL SERVER – 2005 – Database Table Partitioning Tutorial – How to Horizontal Partition Database Table [转载] SQL SERVER – 2005 – Introduction to Partitioning 转载:.NET Programming Standards and Naming Conventions [引]:聚集索引与非聚集索引 Trouble Shooting: can not access Excel file using excel control C#中小数点后保留两位小数,四舍五入的函数及使用方法 关于锁 IE7下关闭窗口不弹出提示窗口方法 [转]ASP.NET如何在客户端调用服务端代码 [转]谈谈Cookie存取和IE页面缓存的问题 Operate File [转] 客户端的JavaScript脚本中获取服务器端控件的值 及ID BULK INSERT Temporary Tables [转]调用.NET XML Web Services返回数据集合
Zhuanzai: change Asp.net Themes dynamicly (Setting An ASP.NET Theme in the PreInit Event Handler)
Edison Zhu · 2010-09-08 · via 博客园 - Edison Zhu

Original article: http://odetocode.com/blogs/scott/archive/2006/03/12/setting-an-asp-net-theme-in-the-preinit-event-handler.aspx 

Let’s say I want the user to select their favorite theme for my application. I can make a list of available themes in a DropDownList control.

<asp:DropDownList ID="_themeList"
                  
runat="server"
                  
AutoPostBack="True">
  <asp:ListItem>Default</asp:ListItem>
  <asp:ListItem>Odeish</asp:ListItem>
  <asp:ListItem>Codeish</asp:ListItem>
</
asp:DropDownList>

I know I must set the Theme property before or during the page’s PreInit event.

What’s wrong with the following code?

protected void Page_PreInit(object sender, EventArgs e)
{
    
if (IsPostBack)
    {
        Theme = _themeList.SelectedValue;
    }
}

The above code throws a null reference exception. PreInit fires before the page instantiates its controls, so _themeList is null (Nothing). I can’t use the _themeList control, but I can go directly to the Request.Form collection. The DropDownList (an HTML select) will post its new value into the form collection.

What could go wrong with this code?

protected void Page_PreInit(object sender, EventArgs e)
{
    
if (IsPostBack)
    {
        
if (Request[_themeListID] != null)
        {
            Theme = Request[_themeListID];
        }
    }
}
const string _themeListID = "themeListID";

Hint: this code will work for many webforms, but not if you are using a master page.

The problem with asking for “_themeList” is that “_themeList” is a server side ID. The browser will submit the form with a unique client side ID. If the DropDownList ends up inside an INamingContainer, the UniqueID property is not the same as the ID property (see my FindControl article for more on INamingContainer). If the DropDownList is on a master page, the UniqueID might look like “ctl00$_themeList”. If the DropDownList is inside a ContentPlaceHolder control, the UniqueID might look like "ctl00$ContentPlaceHolder1$_themeList" .

There are many ways to solve the problem. One solution might be a brute force search of the form collection for a key ending with “_themeList”. Another approach is to stash the list's UniqueID into a hidden form field.

protected void Page_PreInit(object sender, EventArgs e)
{
    
if (IsPostBack)
    {
        
string uniqueID = Request[_themeListIDKey];if (uniqueID != null && Request[uniqueID] != null)
        {
            Theme = Request[uniqueID];
        }
    }
}
protected void Page_Load(object sender, EventArgs e)
{
    ClientScript.RegisterHiddenField(
            _themeListIDKey, _themeList.UniqueID
        );
}
const string _themeListIDKey = "_themeListIDKey";

I’m sure you can think of some other elegant ways to solve the problem. The trick, as always, is finding out what the problem really is.