Zhuanzai: change Asp.net Themes dynamicly (Setting An ASP. NET Theme in the PreInit Event Handler)

Source: Internet
Author: User

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 cocould 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 your webforms, but not if you are using a master page.

The problem with asking for "_ themeList" is that "_ themeList" isServer sideID. The browser will submit the form with a uniqueClient sideID. 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 always 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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.