5

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

MasterPage.master.cs looks like

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

Default.aspx looks like

<%@ 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

using…
public partial class _Default : System.Web.UI.Page
{ 
    protected void Update_Content_Page()
    {
        Label1.Text=”Hello world”;
    }
}
5
  • Here is a tutorial on your request: Interacting with the Content Page from the Master Page I don't paste exact code here since the solution is rather complex. Nov 17, 2011 at 11:01
  • 1
    possible duplicate of Call Method in Master Page Nov 17, 2011 at 11:10
  • 2
    It is not. I am asking how to call content Page function from Master Page not the other way round. Nov 17, 2011 at 11:35
  • I can see why you didn't put any code in here. Solution is complex, but lines of code are few. I tweaked my code little bit and it worked. But, o boy, it wasn't straightforward at all. I accept it as an answer because information on your link helped me to solve my problem. Nov 17, 2011 at 12:05
  • @PavelNefyodov Please post the Code that you succeeded with. I have the same Problem too.. Nov 22, 2012 at 9:32

3 Answers 3

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.

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

2
  • Partially agree with you. In 90% of cases it would work exatly as you said. But there is no such thing as "one size fits all". Please see example that was provided in one of the answers below: asp.net/master-pages/tutorials/… Nov 17, 2011 at 11:43
  • Yes, you are right :) Sometimes you need to change the rules a bit to get the result you want. But it is important to know when you do this, and why :) Nov 17, 2011 at 12:05
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:

HTML:

enter code here

.master jquery code:

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'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.