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

推荐订阅源

美团技术团队
D
DataBreaches.Net
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
Docker
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Check Point Blog
腾讯CDC
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
IT之家
IT之家
月光博客
月光博客
U
Unit 42
K
Kaspersky official blog
T
Threatpost
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
GbyAI
GbyAI
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
云风的 BLOG
云风的 BLOG
酷 壳 – CoolShell
酷 壳 – CoolShell
I
InfoQ
Engineering at Meta
Engineering at Meta
Recorded Future
Recorded Future
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security @ Cisco Blogs
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
Security Archives - TechRepublic
Security Archives - TechRepublic
Webroot Blog
Webroot Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Schneier on Security
S
Secure Thoughts
The Register - Security
The Register - Security
B
Blog RSS Feed
The Last Watchdog
The Last Watchdog
P
Palo Alto Networks Blog
爱范儿
爱范儿
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
L
LINUX DO - 热门话题
C
Cisco Blogs
Spread Privacy
Spread Privacy
F
Full Disclosure
博客园 - 聂微东
T
The Blog of Author Tim Ferriss

博客园 - 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.