Tworzenie strony bazowy, który można dziedziczyć wszystkie strony z i ustawić motyw w przypadku 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();
}
}
Poniżej ThemeSwitcher klasy narzędzie, które obsługuje pobierania/zapisywania aktualnego motywu wystawianie tematów . Ponieważ powiedziałeś, że nie korzystasz z bazy danych, możesz użyć Sesja:
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();
}
}
Będziesz potrzebować strony, na której możesz zmienić temat. Dodaj dropdownlist z następującego kodu za:
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");
}
}
Można pobrać przykładową aplikację here.
Motywy, strony wzorcowe, strony treści i web.config to asp.net, a nie klasyczny asp. Edytowałeś swoje tagi. –
dziękuję, zapomniałem o tym :) –
Istnieje wiele przykładów w Internecie, oto jeden: http://www.asp.net/web-forms/videos/how-do-i/how-do- i-create-user-selectable-themes-for-a-web-site Polecam po prostu przeszukiwać i wyglądać, Microsoft opublikował sporo – John