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”;
    }
}
Pavel Nefyodov
  • 876
  • 2
  • 11
  • 29
  • 1
    possible duplicate of [Call Method in Master Page](http://stackoverflow.com/questions/6332889/call-method-in-master-page) – Grant Thomas Nov 17 '11 at 11:10
  • 2
    It is not. I am asking how to call content Page function from Master Page not the other way round. – Pavel Nefyodov Nov 17 '11 at 11:35
  • @PavelNefyodov Please post the Code that you succeeded with. I have the same Problem too.. – Krishna Thota Nov 22 '12 at 09:32
  • Here is a tutorial on your request: [Interacting with the Content Page from the Master Page](http://www.asp.net/master-pages/tutorials/interacting-with-the-content-page-from-the-master-page-cs) I don't paste exact code here since the solution is rather complex. – Maxim V. Pavlov Nov 17 '11 at 11:01
  • 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. – Pavel Nefyodov Nov 17 '11 at 12:05

3 Answers3

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.

Glory Raj
  • 17,397
  • 27
  • 100
  • 203
  • It might work, but this approach is way too complex. I'll try it next time. Maybe:) But thank you very much anyway. – Pavel Nefyodov Nov 17 '11 at 12:07
  • Can you Please elaborate I can not Understand ?? Check this - http://stackoverflow.com/questions/13508855/accessing-content-page-method-from-masterpage-method – Krishna Thota Nov 23 '12 at 05:48
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'.

Fortin
  • 152
  • 2
  • 14
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

Eystein Bye
  • 5,016
  • 2
  • 20
  • 18
  • 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: http://www.asp.net/master-pages/tutorials/interacting-with-the-content-page-from-the-master-page-cs – Pavel Nefyodov Nov 17 '11 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 :) – Eystein Bye Nov 17 '11 at 12:05