2010-06-23 12 views

risposta

6

È possibile farlo su Page_PreInitas explained here:

protected void Page_PreInit(object sender, EventArgs e) 
{ 
    switch (Request.QueryString["theme"]) 
    { 
     case "Blue": 
      Page.Theme = "BlueTheme"; 
      break; 
     case "Pink": 
      Page.Theme = "PinkTheme"; 
      break; 
    } 
} 
+0

@questo. __curious_geek, perché preferisci farlo in Page_Load not Pre_Int? –

1

mantenere una pagina di base comune per tutte le pagine ASP.NET e modificare la proprietà tema tra ogni caso dopo PreInit o prima della Page_Load nella pagina di base. Questo costringerà ogni pagina ad applicare quel tema. Come in questo esempio, crea MyPage come pagina di base per tutta la tua pagina asp.net.

public class MyPage : System.Web.UI.Page 
{ 
    /// <summary> 
    /// Initializes a new instance of the Page class. 
    /// </summary> 
    public Page() 
    { 
     this.Init += new EventHandler(this.Page_Init); 
    } 


    private void Page_Init(object sender, EventArgs e) 
    { 
     try 
     { 
      this.Theme = "YourTheme"; // It can also come from AppSettings. 
     } 
     catch 
     { 
      //handle the situation gracefully. 
     } 
    } 
} 

//in your asp.net page code-behind 

public partial class contact : MyPage 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
} 
+0

Non farlo in Page_Load, ma in 'PreInit'. –

+0

a destra. Aggiornata la risposta. Grazie. –

3

Si tratta di una risposta molto tardi, ma credo che vi piace questo ..

È possibile modificare il tema della pagina nell'evento PreInit, ma non si utilizza una pagina di base.

Nella pagina master creare un menu a discesa denominato ddlTema, quindi scrivere questo blocco di codice nel proprio Global.asax. Guarda come funziona la magia :)

public class Global : System.Web.HttpApplication 
{ 

    protected void Application_PostMapRequestHandler(object sender, EventArgs e) 
    { 
     Page activePage = HttpContext.Current.Handler as Page; 
     if (activePage == null) 
     { 
      return; 
     } 
     activePage.PreInit 
      += (s, ea) => 
      { 

       string selectedTheme = HttpContext.Current.Session["SelectedTheme"] as string; 
       if (Request.Form["ctl00$ddlTema"] != null) 
       { 
        HttpContext.Current.Session["SelectedTheme"] 
         = activePage.Theme = Request.Form["ctl00$ddlTema"]; 
       } 
       else if (selectedTheme != null) 
       { 
        activePage.Theme = selectedTheme; 
       } 

      }; 
    }