代码之家  ›  专栏  ›  技术社区  ›  Alan

在单个内容页中重写ASP母版页html?

  •  2
  • Alan  · 技术社区  · 15 年前

    我使用的是ASP母版页,我想向 <body>

    我该怎么做?

    2 回复  |  直到 12 年前
        1
  •  1
  •   AaronSieb    15 年前

    我会在母版页中添加一个公共属性,比如 BodyOnKeyPress . 然后在母版页的PreRender事件中设置body标记的OnKeyPress属性。客户端页面只需要在主程序的PreRender事件触发之前设置这个属性。

    母版页标记:

    <%-- Mark the body tag with runat="server", and give it an ID to reference in code. --%>
    <body id="mainBody" runat="server">
        ...
    </body>
    

    母版页代码隐藏:

    protected void Page_PreRender(...) {
        mainBody.Attributes["onkeypress"] = this.BodyOnKeyPress;
    }
    
    public string BodyOnKeyPress {
        get {
            return ViewState["BodyOnKeyPress"];
        }
        set {
            ViewState["BodyOnKeyPress"] = value;
        }
    }
    
        2
  •  1
  •   BrunoLM    15 年前

    也可以在页面内容中使用脚本。。。我将使用jQuery来简化这个想法

    <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <script type="text/javascript">
        $(function () {
            $(document.body).keypress(function(){});
        });
    </script>
    
    </asp:Content>