第一步:从 http://www.dotnettreats.com/downloadfiles/SampleThemes.zip 下载一个简单你的asp.net应用程序。第二步:把zip释放,将其中 App_Themes 复制在您的网站下。第三步:在网站中添加一个ascx文件,然后把下面的代码拷入覆盖:<%@ Control Language="VB"%><script runat="server">    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.Response.Cookies("theme, per user per site").Value = Me.DropDownList1.SelectedValue
        Me.Page.ClientScript.RegisterStartupScript(Me.GetType(), "reload", "setTimout(""window.location.reload()"",1)", True)
    End Sub
</script><asp:DropDownList ID="DropDownList1" runat="server" 
    OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
    <asp:ListItem>MSN_Blue</asp:ListItem>
    <asp:ListItem>MSN_CherryBlossom</asp:ListItem>
    <asp:ListItem>MSN_Finance</asp:ListItem>
    <asp:ListItem>MSN_Morning</asp:ListItem>
    <asp:ListItem>MSN_Purple</asp:ListItem>
    <asp:ListItem>MSN_Red</asp:ListItem>
    <asp:ListItem>WinXP_Blue</asp:ListItem>
    <asp:ListItem>WinXP_Silver</asp:ListItem>
</asp:DropDownList>
如果你喜欢c#代码,可以自己转换一下。不过如果这个可以运行,何必转换呢。第四步:为您的页面从代码源窗口添加一个方法:
    protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);
        var s = Request.Cookies["theme, per user per site"];
        if (s != null && s.Value != null)
            this.Theme = s.Value;
    }
第五步:为您的页面从设计窗口用鼠标拖入上面的ascx。
好了,这就可以运行了。您的页面在运行时就可以选择并记住用户的theme。如果您将第四步的代码在其它页面也增加,或者作为父类继承,那么那些页面就自动变化了。在您开发好asp.net程序之后,再让美工去编辑theme文件,界面的这类设计工作可以放到客户售后服务环节。开发时不要过分关注样式。

解决方案 »

  1.   

    哦,Sorry,我有一个大小写搞错了,是     window.location.Reload()而不是reload(),麻烦请改过来。也可以写为      window.location.href=window.location.href两个的行为稍有不同,后者并不提交页面值,有时候更好一些。
      

  2.   

    哦,对不起我太疏忽了,丢了一个字母“e”,另外一个“;”号诶。那一行应该为:        Me.Page.ClientScript.RegisterStartupScript(Me.GetType(), "reload", "setTimeout(""window.location.href=window.location.href"",1);", True)
    麻烦自己重新复制这一行吧。
      

  3.   

    就是根据cookie读取预设的theme值,这个不用例子也可以啊
      

  4.   

    呵呵,最后修改了一点的ascx是:<%@ Control Language="VB"%><script runat="server">    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
            Me.Response.Cookies("theme, per user per site").Value = Me.DropDownList1.SelectedValue
            Me.Page.ClientScript.RegisterStartupScript(Me.GetType(), "reload", "setTimeout(""window.location.href=window.location.href"",1);", True)
        End Sub
    </script><asp:DropDownList ID="DropDownList1" runat="server" 
        OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
        <asp:ListItem></asp:ListItem>
        <asp:ListItem>MSN_Blue</asp:ListItem>
        <asp:ListItem>MSN_CherryBlossom</asp:ListItem>
        <asp:ListItem>MSN_Finance</asp:ListItem>
        <asp:ListItem>MSN_Morning</asp:ListItem>
        <asp:ListItem>MSN_Purple</asp:ListItem>
        <asp:ListItem>MSN_Red</asp:ListItem>
        <asp:ListItem>WinXP_Blue</asp:ListItem>
        <asp:ListItem>WinXP_Silver</asp:ListItem>
    </asp:DropDownList>
      

  5.   

    将这样一个控件先放在哪怕是空白的页面上,每当你运行页面时都可以点它玩。留着这个东西,开发页面就可以集中在布局上,不会花过多的时间去调整或者担心style方面的东西。
      

  6.   

    很好很好,我是从asp转过来的,现在学asp.net时还是习惯先设计页面,
    你的帖子对我启发很大
      

  7.   

    theme存在cookie里还是有好处,我们的存到Profile里
      

  8.   

    一直为 THMEM 烦恼。 
      

  9.   

    学习,也可以在HttpModule里设置PreInit事件,这样就不用在每个页面里写这个事件了。
    呵呵,我是用Profile做的。没用Cookies
      

  10.   

    我个人比较倾向于css样式表,不知道为什么我总觉得css样式表更方便一些,呵呵。
      

  11.   

    sp1234强人......
    下面这句代码太关键了,采用脚本刷新页面以触发OnPreInit事件
    Me.Page.ClientScript.RegisterStartupScript(Me.GetType(), "reload", "setTimout(""window.location.reload()"",1)", True)