http://www.codeproject.com/useritems/dynamicThemes.asp 可以下載Demo,有源碼,也有Article,應該就是你所需要的

解决方案 »

  1.   

    Page.Theme = Request.QueryString["DropDownList1"];
    这一句有问题吧,代码能详细点吗?
      

  2.   

    在 Page_PreLoad内做这些事情
      

  3.   

    to xiahouwen(武眉博<活靶子.NET>) 
    我看MSDN上说只能在 Page_PreInit内设置主题啊
      

  4.   

    一般在页面基类实现这个功能,这样只要每个页面都继承整个基类就可以变换theme。
    public class BasePage : System.Web.UI.Page
       {
         protected override void OnPreInit(EventArgs e)
         {
            ……
            ……
            base.OnPreInit(e);
         }
       }
      

  5.   

    嘿,认真点,“Request.QueryString["DropDownList1"];”这跟“DropDownList1”控件有关系吗?认真。不会就多查联机帮助。
      

  6.   

    我照书上打的代码啊。
    OK,我把代码贴全了
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ThemeDemo.aspx.cs" Inherits="ThemeDemo"  StylesheetTheme="RedTheme" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Theme Demos</title>
    </head>
    <body>
        <form id="form1" runat="server">      <asp:Label ID="Label3" runat="server" Text="选择页面主题"></asp:Label>&nbsp; &nbsp;
          
          <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
            <asp:ListItem Value="YellowTheme" Selected="True">Yellow</asp:ListItem>
            <asp:ListItem Value="RedTheme">Red</asp:ListItem>
          </asp:DropDownList>
          <br /><br /><br />
            <table class="tableStyle">
                <tr>
                    <td style="height:23px" colspan="2" class="tdStyle"></td>
                </tr>
                <tr>
                    <td style="width: 160px; text-align: right">
                        <asp:Label ID="Label1" runat="server" Text="您的名字"></asp:Label></td>
                    <td style="width: 100px">
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td style="width: 160px; text-align: right">
                        <asp:Label ID="Label2" runat="server" Text="您的昵称"></asp:Label></td>
                    <td style="width: 100px">
                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td colspan="2" style="text-align: center">
                        <asp:Button ID="Button1" runat="server" Text="Button" />
                        &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
                        <asp:Button ID="Button2" runat="server" Text="Button" /></td>
                </tr>
                <tr>
                    <td style="height:23px" colspan="2" class="tdStyle"></td>
                </tr>
            </table>
        </form>
    </body>
    </html>using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class ThemeDemo : System.Web.UI.Page
    {
        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
            this.Theme = Request.QueryString["DropDownList1"];
        }
        protected void Page_Load(object sender, EventArgs e)
        {    }
    }
    问题就是选择后不更新主题。
      

  7.   

    上面的.cs文件内容错了,我重新贴一下using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class ThemeDemo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }    protected void Page_PreInit(object sender, EventArgs e)
        {
            Page.Theme = Request.QueryString["DropDownList1"];
        }
    }
      

  8.   

    Request.QueryString["DropDownList1"]根本不能取得DropDownList1的值,这个写法去取得控件值,毫无道理,只能解释为睡着觉了,没有一点点合理的解释。另外,即使处理了以上问题,从asp.net的设计上说,必须实现为postback,保存页面状态。不能用那种页面重定向的做法。照你这里写法,可以写:    protected void Page_PreInit(object sender, EventArgs e)
        {
            if(Session["theme"]!=null)
            Page.Theme = (string)Session["theme"];
        }    void DropDownList1_SelectedIndexChanged(object sender,.....)
        {
            Session["theme"]=DropDownList1.SelectedValue;
            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "reload",
                this.Page.ClientScript.GetPostBackEventReference(this,null), true);
        }不一定使用Session。这里仅仅是偷懒举例而已。实际情况中,往往还可以使用数据库、cookie等。
      

  9.   

    protected void Page_PreInit(object sender, EventArgs e)
        {
            if (Session["Theme"] != null)
            {
                Page.Theme = Session["Theme"].ToString();
            }
            else
            {
                Session["Theme"] = "YellowTheme";
                Page.Theme = Session["Theme"].ToString();
            }
        }    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["Theme"] = RadioButtonList1.SelectedValue;
        }我晕了,我换了个控件RadioButtonList1,里面有两个item
    text:Yellow  value:YellowTheme
    text:Red  value:RedTheme现在能换主题了,可出来怪毛病了,我点Yellow,他变成红色,我点Red他变成黄色。晕了,我theme里颜色没有写反,这是为什么吗
      

  10.   

    当你改变RadioButtonList1.SelectedValue时,必须表现它的ui。否则你改变了Session值当时并没有表现它,名不符实,逻辑肯定不对。可是theme规定必须写在PreInit(之前),必须刷新(reload)页面。第三个注意点是:这么简单的一个功能,必须实现为postback,如果实现为页面重定向就太笨拙了(页面上各种业务控件的状态全丢了)。
      

  11.   

    page_init()方法
    永远在
    page_load()方法
    执行之前,所以才出现以上问题!
      

  12.   

    <%@   Page   Language= "C# "   AutoEventWireup= "true "   CodeFile= "ThemeDemo.aspx.cs "   Inherits= "ThemeDemo "     StylesheetTheme= "RedTheme "   %> 中设置了StylesheetTheme= "RedTheme " ,去掉