2010-05-19 111 views

回答

1

我会添加一个公共属性到MasterPage,像BodyOnKeyPress。然后在MasterPage的PreRender事件中设置Body标签的OnKeyPress属性。客户端页面只需要在Master的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; 
    } 
} 
+0

好的,这听起来像一个可行的解决方案。你可以给PreRender事件代码看起来像什么指针?我想我可以弄清楚剩下的东西(我是ASP Masterpages新手)。 – Alan 2010-05-19 02:27:34

+1

@Alan - 我用一个例子更新了我的答案。它是空运码,但它应该显示基本结构。 – AaronSieb 2010-05-19 02:35:49

+0

空码,我喜欢这样。 – Alan 2010-05-19 02:38:27

1

也有一个脚本在该页面的内容做...我会使用jQuery来简化想法

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

</asp:Content>