2010-05-19 107 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>