[英]how to call the master page method in content page but which is not inheritted from the master page
[英]How to call content Page function from Master Page
需要從母版頁調用內容頁函數。 如果需要更多數據,請告訴我。
MasterPage.master.cs 看起來像
protected void Required_Function(object sender, EventArgs e)
{
// call Update_Content_Page() from content page
}
Default.aspx 看起來像
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="contentPlaceHolder" Runat="Server">
<asp:Label ID="Label1" runat="server" Text="Label">Hello people!</asp:Label>
</asp:Content>
Default.aspx.cs 看起來像
using…
public partial class _Default : System.Web.UI.Page
{
protected void Update_Content_Page()
{
Label1.Text=”Hello world”;
}
}
你可以像這樣嘗試......不完全但會幫助你......
您可以從基類繼承您的頁面。 然后,您可以在基類中創建一個虛擬方法,該方法將在您的頁面中被覆蓋。 然后,您可以像這樣從母版頁調用該虛擬方法 -
(cphPage.Page as PageBase).YourMethod();
此處, cphPage
是母版頁中ContentPlaceHolder
的 ID。 PageBase
是包含YourMethod
方法的基類。
我個人使用 jquery 做了一個技巧:當我點擊主按鈕時,它實際上點擊了名為“saveButton”的內容頁面按鈕並使用了它的功能:
HTML:
enter code here
.master jquery 代碼:
function tester() {
console.log("Testing");
$("[id$='SaveButton']").click();
}
我使用 id$='SaveButton' 原因正如您現在可能知道的那樣,當控件位於母版、轉發器、網格視圖和其他包含控件中時,ASP.NET 會重命名控件。 $id='stuff' 驗證控件 ID 是否以 'stuff' 結尾。
我通常發現,當 MasterPage 需要調用 ContentPage 中的函數時,您的頁面設計存在缺陷。 MasterPage 不需要知道關於 ContentPages 的任何信息。 但是如果你覺得這對你來說是正確的方法,這里是 CodeProject 的 指南
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.