在单个内容页面中覆盖ASP Master Page html?

时间:2010-05-19 02:19:39

标签: asp.net .net html master-pages

我正在使用ASP Master页面,我想在Master页面的<body>添加一个“onkeypress”事件处理程序,但仅限于一个页面。

我该怎么做?

2 个答案:

答案 0 :(得分:1)

我会在MasterPage中添加一个公共属性,类似于BodyOnKeyPress。然后在MasterPage的PreRender事件中设置body标签的OnKeyPress属性。客户端页面只需要在Master的PreRender事件触发之前设置此属性。

这是航空代码,因为我没有要测试的项目。但它应该是这样的:

MasterPage标记:

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

MasterPage CodeBehind:

protected void Page_PreRender(...) {
    mainBody.Attributes["onkeypress"] = this.BodyOnKeyPress;
}

public string BodyOnKeyPress {
    get {
        return ViewState["BodyOnKeyPress"];
    }
    set {
        ViewState["BodyOnKeyPress"] = value;
    }
}

答案 1 :(得分:1)

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

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

</asp:Content>