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

來源:互聯網
上載者: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 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.

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.