如何从母版页调用内容页面功能

时间:2021-03-23 21:09:55

It is necessary to call content Page function from Master Page.Please let me know if more data needed.

有必要从母版页调用内容页面功能。如果需要更多数据,请告诉我。

MasterPage.master.cs looks like

MasterPage.master.cs看起来像

 protected void Required_Function(object sender, EventArgs e) {    // call Update_Content_Page() from content page  }

Default.aspx looks like

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 looks like

Default.aspx.cs看起来像

using…public partial class _Default : System.Web.UI.Page{     protected void Update_Content_Page()    {        Label1.Text=”Hello world”;    }}

3 个解决方案

#1


6  

you can try like this.. not exactly but will helps you.....

你可以尝试这样..不完全但会帮助你.....

You can inherit your page from a base class. Then you can create a virtual method in your base class which will get overridden in your page. You can then call that virtual method from the master page like this -

您可以从基类继承页面。然后,您可以在基类中创建一个虚拟方法,该方法将在页面中被覆盖。然后,您可以从主页面调用该虚拟方法,如下所示 -

(cphPage.Page as PageBase).YourMethod();

Here, cphPage is the ID of the ContentPlaceHolder in your master page. PageBase is the base class containing the YourMethod method.

这里,cphPage是主页面中ContentPlaceHolder的ID。 PageBase是包含YourMethod方法的基类。

#2


1  

I usually find that when the MasterPage needs to call a function in a ContentPage you have a flaw in the design of your page. The MasterPage should not need to know anything about the ContentPages. But if you feel that this is the right way for you here is a guide from CodeProject

我经常发现当MasterPage需要在ContentPage中调用函数时,您的页面设计存在缺陷。 MasterPage不需要知道任何有关ContentPages的信息。但是,如果您觉得这是正确的方法,请参阅CodeProject的指南

#3


1  

Personally i did a trick using jquery: When i clicked on the master button, it actually clicked on the content page button named 'saveButton' and used its function:

我个人使用jquery做了一个技巧:当我点击主按钮时,它实际上点击了名为'saveButton'的内容页面按钮并使用了它的功能:

HTML:

enter code here

.master jquery code:

.master jquery代码:

function tester() {        console.log("Testing");        $("[id$='SaveButton']").click();    }

I used id$='SaveButton' cause as you might know now, ASP.NET renames controls when they are inside a master, a repeater, a grid view, and other containing controls. $id='stuff' validates that the control ID ends with 'stuff'.

我使用了id $ ='SaveButton',因为你现在可能已经知道,当ASP.NET在主控,转发器,网格视图和其他包含控件内时,它会重命名控件。 $ id ='stuff'验证控件ID以'stuff'结尾。

#1


6  

you can try like this.. not exactly but will helps you.....

你可以尝试这样..不完全但会帮助你.....

You can inherit your page from a base class. Then you can create a virtual method in your base class which will get overridden in your page. You can then call that virtual method from the master page like this -

您可以从基类继承页面。然后,您可以在基类中创建一个虚拟方法,该方法将在页面中被覆盖。然后,您可以从主页面调用该虚拟方法,如下所示 -

(cphPage.Page as PageBase).YourMethod();

Here, cphPage is the ID of the ContentPlaceHolder in your master page. PageBase is the base class containing the YourMethod method.

这里,cphPage是主页面中ContentPlaceHolder的ID。 PageBase是包含YourMethod方法的基类。

#2


1  

I usually find that when the MasterPage needs to call a function in a ContentPage you have a flaw in the design of your page. The MasterPage should not need to know anything about the ContentPages. But if you feel that this is the right way for you here is a guide from CodeProject

我经常发现当MasterPage需要在ContentPage中调用函数时,您的页面设计存在缺陷。 MasterPage不需要知道任何有关ContentPages的信息。但是,如果您觉得这是正确的方法,请参阅CodeProject的指南

#3


1  

Personally i did a trick using jquery: When i clicked on the master button, it actually clicked on the content page button named 'saveButton' and used its function:

我个人使用jquery做了一个技巧:当我点击主按钮时,它实际上点击了名为'saveButton'的内容页面按钮并使用了它的功能:

HTML:

enter code here

.master jquery code:

.master jquery代码:

function tester() {        console.log("Testing");        $("[id$='SaveButton']").click();    }

I used id$='SaveButton' cause as you might know now, ASP.NET renames controls when they are inside a master, a repeater, a grid view, and other containing controls. $id='stuff' validates that the control ID ends with 'stuff'.

我使用了id $ ='SaveButton',因为你现在可能已经知道,当ASP.NET在主控,转发器,网格视图和其他包含控件内时,它会重命名控件。 $ id ='stuff'验证控件ID以'stuff'结尾。