Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.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# 如何使用下拉列表为所有网页设置网站主题?_C#_Themes - Fatal编程技术网

C# 如何使用下拉列表为所有网页设置网站主题?

C# 如何使用下拉列表为所有网页设置网站主题?,c#,themes,C#,Themes,我正在尝试使用我的主页上的下拉列表来选择和设置所有网页的主题。它设置为主页,但当我转到任何其他页面时,它没有主题。这是我的主页代码: protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { string selectedTheme = Page.Theme; HttpCookie userSe

我正在尝试使用我的主页上的下拉列表来选择和设置所有网页的主题。它设置为主页,但当我转到任何其他页面时,它没有主题。这是我的主页代码:

        protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string selectedTheme = Page.Theme;
            HttpCookie userSelectedTheme =
            Request.Cookies.Get("UserSelectedTheme");

            if (userSelectedTheme != null)
            {
                selectedTheme = userSelectedTheme.Value;
            }

            if (!string.IsNullOrEmpty(selectedTheme) &&
            ddlSetTheme.Items.FindByValue(selectedTheme) != null)
            {
                ddlSetTheme.Items.FindByValue(selectedTheme).Selected =
                true;
            }
        }
    }

    protected void ddlSetTheme_SelectedIndexChanged(object sender, EventArgs e)
    {
        HttpCookie userSelectedTheme = new
        HttpCookie("UserSelectedTheme");
        userSelectedTheme.Expires = DateTime.Now.AddMonths(6);
        userSelectedTheme.Value = ddlSetTheme.SelectedValue;
        Response.Cookies.Add(userSelectedTheme);
        Response.Redirect(Request.Url.ToString());
    }

    private void Page_PreInit(object sender, EventArgs e)
    {
        HttpCookie setTheme = Request.Cookies.Get("UserSelectedTheme");
        if (setTheme != null)
        {
            Page.Theme = setTheme.Value;
        }
    }

我认为我拥有的代码只足以将主题应用于一个页面,那么如何将其应用于所有页面?

默认情况下,您的HttpCookie的作用域就是您所在的页面

如果希望它是整个域,则需要将路径设置为整个站点,可能如下所示:

userSelectedTheme.Path = "/";
更多信息:


我所要做的就是将preinit代码放在其他页面的代码文件中。

为什么不使用母版页并为此设置主题?它将自动反映页面1)检查浏览器中的cookie定义并确定范围。范围必须是整个域/网站,cookie才能在站点范围内可用。2) 此外,请尝试在其他页面上输出cookie值,并确认它被正确检索。