2010-06-23 20 views
5

एक एएसपी.NET एप्लिकेशन की कल्पना करें जिसमें कई थीम परिभाषित हैं। मैं गतिशील रूप से कुल एप्लिकेशन (केवल एक पृष्ठ नहीं) की थीम कैसे बदल सकता हूं। मुझे पता है कि यह web.config में संभव है। लेकिन मैं इसे गतिशील रूप से बदलने में सक्षम होना चाहता हूं। मैं यह कैसे shpuld करता हूँ?गतिशील रूप से कुल एएसपी.NET अनुप्रयोग की थीम कैसे बदलें?

अग्रिम

उत्तर

6

आप 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

@ थीसिस। __curious_geek, पेज_लोड में ऐसा क्यों करना पसंद नहीं है Pre_Int? –

1

धन्यवाद सभी अपने asp.net पृष्ठों के लिए एक आम आधार पेज रखने के लिए और PreInit के बाद या आधार पेज में Page_Load से पहले किसी भी घटना के बीच विषय संपत्ति को संशोधित। यह प्रत्येक पृष्ठ को उस थीम को लागू करने के लिए मजबूर करेगा। इस उदाहरण के रूप में MyPage को अपने सभी एएसपीनेट पेज के लिए बेस पेज के रूप में बनाएं।

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

पेज_लोड में ऐसा न करें, लेकिन 'प्रीइनिट' में। –

+0

सही। उत्तर अपडेट किया गया। धन्यवाद। –

3

यह एक बहुत देर से इस सवाल का जवाब है, लेकिन मुझे लगता है कि आपको यह पसंद आएगा ..

आप पृष्ठ के विषय को बदल सकते हैं प्रीइनिट इवेंट में, लेकिन आपने बेस पेज का उपयोग नहीं किया है ..

मास्टरपेज में डीडीएलटीमा नामक एक ड्रॉपडाउन बनाएं, उसके बाद यह कोड आपके ग्लोबल.एक्सएक्स में लिखें। देखें कि जादू कैसे काम करता है :)

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; 
       } 

      }; 
    } 
संबंधित मुद्दे