6

いくつかのテーマが定義されたASP.NETアプリケーションを想像してみてください。(単一ページだけでなく)アプリケーション全体のテーマを動的に変更するにはどうすればよいですか。私はそれがで可能であることを知ってい<pages Theme="Themename" />ますweb.config。しかし、私はそれを動的に変更できるようにしたいと思っています。どのように私はそれをしますか?

前もって感謝します

4

3 に答える 3

6

Page_PreInit ここで説明されているようにこれを行うことができます:

protected void Page_PreInit(object sender, EventArgs e)
{
    switch (Request.QueryString["theme"])
    {
        case "Blue":
            Page.Theme = "BlueTheme";
            break;
        case "Pink":
            Page.Theme = "PinkTheme";
            break;
    }
}
于 2010-06-23T06:50:17.703 に答える
3

非常に遅い答えですが、きっと気に入ると思います。

PreInitイベントでページのテーマを変更できますが、ベースページを使用する必要はありません。

マスターページで、ddlTemaという名前のドロップダウンを作成し、その後、Global.asaxにこのコードブロックを記述します。魔法のしくみをご覧ください:)

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

            };
    }
于 2012-04-11T14:40:56.450 に答える
1

すべてのasp.netページに共通のベースページを保持し、ベースページのPreInit前後のイベント間でテーマプロパティを変更しPage_Loadます。これにより、各ページにそのテーマが適用されます。この例のように、すべてのasp.netページのベースページとして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)
    {

    }
}
于 2010-06-23T06:48:55.723 に答える