看到别人的网站的主题都是在后台进行切换,我自已做了一下,只能在本页面切换。
page.Theme="RedTheme"
有什么方法能通过后台操作而进行前台的主题节换吗?

解决方案 »

  1.   

    除了一般在body内进行主题的切换外,还有一种通过程序控制,使用dropdownlist来实现主题的切换。1.首先建立三个主题,分别为default,主题2,主题3,放在App_Themes内。这里的示例,添加一个样式表stylesheet1,只设置了gridview1的border属性。其中default为red,主题2为green,主题3为purple.#GridView1
    {
     border:solid 1px red;
     }2.建一个基类GRbase,将其设置由public class GRbase:System.Web.UI.Page 派生。代码如下:public class GRbase:System.Web.UI.Page
    {
     public GRbase()
     {
      //
      // TODO: 在此处添加构造函数逻辑
      //
     }
       protected override void  OnPreInit(EventArgs e)//在页面初始化之前进行调用
         {     
             HttpCookie hc = Request.Cookies["Sessiontheme"];//接收cookies传值;
             if (hc != null)
             {
                 try
                 {
                     Page.Theme = Server.HtmlEncode(hc.Value.ToString());//有值是使用传过来的值;
                 }
                 catch
                 {
                     Page.Theme = "Default";
                 }
             }
             else
             {
                 Page.Theme = "Default";//否则默认值为default;
             }
             base.OnPreInit(e);
         }    public static System.Collections.ArrayList Themeal;//设置静态的动态数组;    public static void Init()
        {
            Themeal = new System.Collections.ArrayList();
            System.IO.DirectoryInfo df = new System.IO.DirectoryInfo(HttpContext.Current.Server.MapPath("App_Themes"));//得到App_Themes下的文件夹;
            foreach (System.IO.DirectoryInfo dr in df.GetDirectories())
            {
                Themeal.Add(dr.Name);//逐个读取文件夹的名称,并将其添加到arraylist中;
            }
        } 3.添加一个Global.asax文件,对GRbase的Init方法进行调用,代码如下:   void Application_Start(object sender, EventArgs e)
        {
            // 在应用程序启动时运行的代码
            GRbase.Init();
        }意为,在这个网站一开始运行时就调用这个方法。4.代码视图状态下:public partial class Default2 :GRbase//页面须从基类中派生出来,否则基类中的设置在此无效。protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string sql = "select * from stus";
                DataTable dt = Class1.Gettb(sql);
                this.GridView1.DataSource = dt;//绑定的数据库的值
                this.GridView1.DataBind();
                dropdl();
            }
        }
        private void dropdl()
        {
            try
            {
                this.DropDownList1.DataSource = GRbase.Themeal;//从基类中调用arraylist数组,并绑定到dropdownlist中;
                this.DropDownList1.DataBind();
                this.DropDownList1.SelectedValue = this.Theme;//设置默认值为当前的主题;
            }
            catch
            {
            }
        }    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
          HttpCookie hc = new HttpCookie("Sessiontheme");//当dropdownlist发生改变时,写一个cookie;
            hc.Value = DropDownList1.SelectedValue;//dropdownlist的选中项,赋给cookie;
            Response.Cookies.Add(hc);//用response写入;
            Response.Redirect("Default2.aspx");//跳转本网页时,会调用GRbase的onpreinit,来获取cookie的值;
        }
      

  2.   

    可以在Page_PreInit事件中编写
    page.Theme="RedTheme"进行皮肤切换.
    当然你可以用ViewStateage保存Page.Theme指向的主题皮肤,这样通过修改ViewState中的值就可修改主题皮肤了