动态切换样式表

时间:2017-09-11 12:11:45

标签: c# css asp.net

我的任务是合并两个页面。我使用ASP.NET。在加载时,我必须选择.css,一个业务对象并在asp:updatePanel中填充数据。这两个站点应显示来自两个不同设备的日志。

据我了解,我应该在Page_Load中切换所有内容。

我试图在同一时间在Page_PreInt和Page_Load中选择css文件,但这两个文件都不起作用:

protected void Page_PreInit(object sender, EventArgs e)
    {

        // seting the master page
        switch (deviceOrTerminal) 
        {
            case DeviceOrTerminalEnum.Device:
                this.Master.MasterPageFile = "~/Device/Device.Master";
                Session.Add("style", "/Theme/DeviceEventHistory.css");
                lnkCSS.Attributes["href"] = "/Theme/DeviceEventHistory.css";
                lnkCSS.Attributes["href"] = "~/Theme/DeviceEventHistory.css";
                break;
            case DeviceOrTerminalEnum.Terminal:
                this.Master.MasterPageFile = "~/Terminal/TerminalDetail.Master";
                Session.Add("style", "/Theme/TerminalEventHistory.css");
                lnkCSS.Attributes["href"] = "/Theme/TerminalEventHistory.css";
                lnkCSS.Attributes["href"] = "~/Theme/TerminalEventHistory.css";
                break;
            case DeviceOrTerminalEnum.None:
                this.Master.MasterPageFile = "~/Main.Master";
                Session.Add("style", "/Theme/NoneEventHistory.css");
                lnkCSS.Attributes["href"] = "/EventHistory/Theme/NoneEventHistory.css";
                lnkCSS.Attributes["href"] = "~/EventHistory/Theme/NoneEventHistory.css";
                break;
        }
    }`


protected void Page_Load(object sender, EventArgs e)
    {
        CommandBarTop.TitleCaption = TitleCaption;
        CommandBarGridTop.DisableAllButtons();
        CommandBarGridBottom.DisableAllButtons();
        CommandBarTop.DisableAllButtons();
        CommandBarBottom.DisableAllButtons();
        CommandBarBottom.SetSearchMode(eventHistoryBo.PermissionId);
        CommandBarBottom.SearchClicked += CommandBar_SearchClicked;
        CommandBarBottom.SearchButtonText = (String)GetLocalResourceObject("CommandBar.SearchButton");
        CommandBarTop.SetSearchMode(eventHistoryBo.PermissionId);
        CommandBarTop.SearchClicked += CommandBar_SearchClicked;
        CommandBarTop.SearchButtonText = (String)GetLocalResourceObject("CommandBar.SearchButton");
        String shortDatePattern = PreferredBrowserLanguage.DateTimeFormat.ShortDatePattern;
        String shortTimePattern = PreferredBrowserLanguage.DateTimeFormat.ShortTimePattern;
        txtEventTimeFrom.CalendarDateFormat = shortDatePattern + " " + shortTimePattern;
        txtEventTimeTo.CalendarDateFormat = shortDatePattern + " " + shortTimePattern;

        String jsValidatorName = "clientValidate";
        Type jsType = GetType();
        if (!Page.ClientScript.IsClientScriptBlockRegistered(jsValidatorName))
        {
            Initialize();
            if (!Page.IsPostBack)
            {
                if ((!TMSPrincipal.IsPermissionOk(deviceBo.PermissionId, EnumPermissionType.View))
                   || (!TMSPrincipal.IsPermissionOk(terminalBo.PermissionId, EnumPermissionType.View)))
                {
                    throw new HttpException("Insufficient permissions to view details.");
                }

                switch (deviceOrTerminal) 
                {
                    case DeviceOrTerminalEnum.Device:
                        CommandBarTop.TitleCaption.Replace(CommandBarTop.TitleCaption.ToString(), "Device");
                        CommandBarGridTop.TitleCaption = "Device";
                        lnkCSS.Attributes["href"] = "/Theme/DeviceEventHistory.css";
                        lnkCSS.Attributes["href"] = "~/Theme/DeviceEventHistory.css";
                        break;
                    case DeviceOrTerminalEnum.Terminal:
                        CommandBarTop.TitleCaption.Replace(CommandBarTop.TitleCaption.ToString(), "Terminal");
                        CommandBarGridTop.TitleCaption = "Terimal";
                        lnkCSS.Attributes["href"] = "/Theme/TerminalEventHistory.css";
                        lnkCSS.Attributes["href"] = "~/Theme/TerminalEventHistory.css";
                        break;
                    case DeviceOrTerminalEnum.None:
                        CommandBarTop.TitleCaption.Replace(CommandBarTop.TitleCaption.ToString(), " ");
                        UserHints.Add(new UserHint(GetLocalResourceObject("Error_DeviceNorTerminal_Title.Text").ToString(), GetLocalResourceObject("Error_DeviceNorTerminal_Body.Text").ToString(), EnumUserHintType.Error));
                        lnkCSS.Attributes["href"] = "/EventHistory/Theme/NoneEventHistory.css";
                        lnkCSS.Attributes["href"] = "~/EventHistory/Theme/NoneEventHistory.css";
                        break;
                }

                txtEventTimeFrom.Text = DateTime.Now.Date.AddDays(-7).ToString("g");
                txtEventTimeTo.Text = DateTime.Now.Date.AddDays(1).ToString("g");
                FillData();
                InitializeDropDownEventType();
            }
        }
    }

我编程这种ASP.NET一个月(在我使用MVC和Razor之前),所以我会感谢任何服务。我知道有一些小问题,但没有一个能解决我的问题。

1 个答案:

答案 0 :(得分:1)

我做了一个简单的测试:

<强> MasterA.Master

...
 <h1>Master A</h1>
...

<强> MasterB.Master

...
 <h1>Master B</h1>
...

<强> Page.aspx

Page.aspx.cs

public partial class Page : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        var letter = HttpContext.Current.Request["m"] != null ?
                     HttpContext.Current.Request["m"] : "A";
        this.MasterPageFile = "~/Master" + letter + ".Master";
    }
}

Page.aspx

<%@ Page Title="" Language="C#" 
    MasterPageFile="~/MasterA.Master" 
    AutoEventWireup="true" 
    CodeBehind="Page.aspx.cs" 
    Inherits="WebAppWebForms.Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <h3>Content</h3>
    <p>Content of the page</p>
</asp:Content>

我得到了这个:

因此,您可以docs mention在页面的PreInit事件中动态更改母版页。

现在,CSS ......

如果您要更改母版页,为什么不在母版页本身中编写正确的CSS文件,而您只需切换母版页即可?

如果您只是切换母版,则可以使用ViewSwitcher编写脚本,因为这是WebForms在为不同设备切换masterPages时所使用的...

在Visual Studio中启动一个全新的WebForms项目,并使用文件ViewSwitcher.ascx

查看他们如何完成该任务