NET,我最近买了一本书开始自己试着按照练习。动态选择主题
我知道你可以通过添加“theme”属性来应用web配置文件的标签中的主题,但是下面的练习是从下拉列表中选择一个我不能很好理解的主题,根本不适合我。
这里是母版页CS码(书代码)
namespace WebApplication7
{
public partial class SiteMaster : MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string selectedTheme = Page.Theme;
HttpCookie preferredTheme = Request.Cookies.Get("PreferredTheme");
if (preferredTheme != null)
{
selectedTheme = preferredTheme.Value;
}
if (!string.IsNullOrEmpty(selectedTheme))
{
ListItem item = ThemeList.Items.FindByValue(selectedTheme);
if (item != null)
{
item.Selected = true;
}
}
}
}
protected void Theme_SelectedIndexChanged(object sender, EventArgs e)
{
HttpCookie preferredTheme = new HttpCookie("PreferredTheme");
preferredTheme.Expires = DateTime.Now.AddMonths(3);
preferredTheme.Value = ThemeList.SelectedValue;
Response.Cookies.Add(preferredTheme);
Response.Redirect(Request.Url.ToString());
}
}
}
这里是下拉列表
<asp:DropDownList ID="ThemeList" runat="server" OnSelectedIndexChanged="Theme_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem Value="monochrome" >Monochrome</asp:ListItem>
<asp:ListItem Value="colors">Colors</asp:ListItem>
</asp:DropDownList>
这是书“开始ASP.NET 4.5.1为例: C#和VB“
本书没有提到任何缺失的步骤,说实话我不明白下拉列表”indexchanged“是如何链接到我在App_Themes文件夹中创建的主题。我只是假设这与“Page.Theme”有关。
在此先感谢。
您可能需要继续阅读,因为发布的示例不会更改当前主题 –
https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work可能有帮助。 – mjwills