How to call content Page function from Master Page
Asked Answered
A

3

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”;
    }
}
Anglim answered 17/11, 2011 at 10:55 Comment(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.Caecum
possible duplicate of Call Method in Master PageOverset
It is not. I am asking how to call content Page function from Master Page not the other way round.Anglim
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.Anglim
@PavelNefyodov Please post the Code that you succeeded with. I have the same Problem too..Vegetarian
D
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.

Dolan answered 17/11, 2011 at 11:1 Comment(2)
It might work, but this approach is way too complex. I'll try it next time. Maybe:) But thank you very much anyway.Anglim
Can you Please elaborate I can not Understand ?? Check this - #13509355Vegetarian
H
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

Hooten answered 17/11, 2011 at 11:11 Comment(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/…Anglim
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 :)Hooten
N
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'.

Ness answered 18/4, 2014 at 13:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.