Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在运行时以编程方式更改asp Web应用程序主题(而不是页面)?_C#_Asp.net_Themes - Fatal编程技术网

C# 如何在运行时以编程方式更改asp Web应用程序主题(而不是页面)?

C# 如何在运行时以编程方式更改asp Web应用程序主题(而不是页面)?,c#,asp.net,themes,C#,Asp.net,Themes,我正在学习ASP.net,我一直在玩主题和母版页。我决定更改网站的主题,并使用了web.config解决方案(将主题添加到web.config)。我现在想做的是能够根据用户和用户选择的主题更改主题 我一直找不到任何教程,它们似乎都显示了如何改变单独的内容页,但我想改变整个网站的主题 你是如何做到这一点的,用最简单的方法?我没有连接到数据库atm,只是为了练习:) 带着亲切的问候我以前看到过从一个基本页面继承所有页面的做法 public partial class _Default : B

我正在学习ASP.net,我一直在玩主题和母版页。我决定更改网站的主题,并使用了web.config解决方案(将主题添加到web.config)。我现在想做的是能够根据用户和用户选择的主题更改主题

我一直找不到任何教程,它们似乎都显示了如何改变单独的内容页,但我想改变整个网站的主题

你是如何做到这一点的,用最简单的方法?我没有连接到数据库atm,只是为了练习:)


带着亲切的问候

我以前看到过从一个基本页面继承所有页面的做法

    public partial class _Default : BasePage
以及在基页类中设置主题

public class BasePage : System.Web.UI.Page
{
  protected override void OnPreInit(EventArgs e)
  {
       base.OnPreInit(e);
       Page.Theme = //Your theme;
  }
}

看看这些有用的MSDN文档:

也请检查此项:

不能直接将ASP.NET主题应用于母版页。如果你 向@Master指令添加一个主题属性,页面将引发 运行时出现错误


创建一个从中继承所有页面的基本页面,并在OnPreInit事件中设置主题:

public class ThemePage : System.Web.UI.Page
{
    protected override void OnPreInit(EventArgs e)
    {
        SetTheme();            

        base.OnPreInit(e);
    }

    private void SetTheme()
    {
        this.Theme = ThemeSwitcher.GetCurrentTheme();
    }
}
下面是ThemeSwitcher实用程序类,用于处理获取/保存当前主题和列表主题。既然您说您没有使用数据库,您可以使用会话:

public class ThemeSwitcher
{
    private const string ThemeSessionKey = "theme";

    public static string GetCurrentTheme()
    {
        var theme = HttpContext.Current.Session[ThemeSessionKey]
            as string;

        return theme ?? "Default";
    }

    public static void SaveCurrentTheme(string theme)
    {
        HttpContext.Current.Session[ThemeSessionKey]
            = theme;
    }

    public static string[] ListThemes()
    {
        return (from d in Directory.GetDirectories(HttpContext.Current.Server.MapPath("~/app_themes"))
                select Path.GetFileName(d)).ToArray();
    }
}
您需要一个可以更改主题的页面。添加包含以下代码的dropdownlist:

public partial class _Default : ThemePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindData();
        }
    }

    private void BindData()
    {
        var currentTheme = ThemeSwitcher.GetCurrentTheme();

        foreach (var theme in ThemeSwitcher.ListThemes())
        {
            var item = new ListItem(theme);
            item.Selected = theme == currentTheme;
            ddlThemes.Items.Add(item);
        }
    }

    protected void ddlThemes_SelectedIndexChanged(object sender, EventArgs e)
    {
        ThemeSwitcher.SaveCurrentTheme(ddlThemes.SelectedItem.Value);
        Response.Redirect("~/default.aspx");
    }       
}

您可以下载示例应用程序。

主题、母版页、内容页和web.config是asp.net,而不是经典的asp.net。编辑你的标签。网上有很多例子,这里有一个:我建议你四处搜索,看看,微软已经发布了很多感谢你,我忘了:)这很有效,但我不得不硬编码controlname,因为他使用的代码给我一个空引用异常。。。你知道为什么吗?(我下载了他的代码,仔细检查后发现了同样的问题)
public类BasePage:Page{public BasePage(){}受保护的覆盖void OnPreInit(EventArgs e){base.OnPreInit(e);if(Request.Form!=null&&Request.Form.Count>0){this.Theme=Request.Form[this.Master.FindControl(“DropDownList1”).UniqueID];}}}}
+1您应该在调用base.OnPreInit(e)之前设置主题。来自MSDN“主题属性必须在PreInit事件之前设置;在PreInit事件之后设置主题属性将导致InvalidOperationException异常。”我在发布之前检查了。这是最近发生的变化还是MSDN得到了冲突的信息。这里有一篇有趣的文章,它说“在”PreInit事件页面个性化和主题应用之后。PreInit事件是页面生命周期早期的一个事件,您可以访问它。在PreInit事件之后,将加载个性化信息和页面主题(如果有)。在OnPagePreInit之前调用page_PreInit?我会试一试,让你知道它是如何进行的:)莱尼尔,教程是关于网页,而不是整个网站。不过还是要谢谢你:)谢谢你的代码,这是唯一有效的方法,我尝试了上面所有的教程!皮尤!:)