How to call content Page function from Master Page

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.


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


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


Comments

  1. Serra

    • 2020/11/17

    You can do this using Delegates. For Example, you have a button in MasterPage and you want to call a Method in Content Page from Master Page. Here is the Code 

  2. Bryan

    • 2018/9/11

    1. 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"

  3. Clark

    • 2018/3/30

    Hi I have a master page with Logout button which is visible across all my content pages.When it is clicked the session should set to null 

  4. Davis

    • 2017/8/26

    Add the following markup beneath the <siteMapNode> for the Content to Master Page Interaction lesson: <siteMapNode url="~/Admin/Products.aspx" title="Master to Content Page Interaction" /> The addition of this <siteMapNode> element is reflected in the Lessons list (see Figure 5). Return to Products.aspx.

  5. Andrew

    • 2020/3/10

    In your master page click event for the button you can cast the Page property to the interface and then call Save on that. C#. Copy Code.

  6. Ulises

    • 2019/2/6

    Considering all your pages uses MasterPage and your function is in a JS file, there are 2 ways: a) Load JS file in header section of MasterPage, JS file will be loaded for any ContentPage; b) Put a ContentPlaceHolder in header section of MasterPage, then load JS file on ContentPage as needed. Reply. mgebhard.

  7. Prishtina

    • 2021/6/25

    Calling Content Page Method from MasterPage Method. Mar 2 2015 10:37 PM. How to call Content Page Method from MasterPage Method???????????? Reply 

  8. Keegan

    • 2016/1/18

    Accept Solution Reject Solution. In your master page click event for the button you can cast the Page property to the interface and then call Save on that. C#. Copy Code. protected void myButton_Click ( object sender, EventArgs e) { ICanSave myPage = this .Page as ICanSave; myPage.Save (); } C#.

  9. Wes

    • 2018/6/2

    cs that I use for public methods. Problem is that it is not a CodeBehind for an ASPX page but just a code file. How to control the master page is the later 

  10. De Santis

    • 2020/2/23

    Examines how to call methods, set properties, etc. of the Master Page from code in the Content Page. Introduction. Over the course of the past five tutorials we have looked at how to create a master page, define content regions, bind ASP.NET pages to a master page, and define page-specific content.

  11. Leonidas

    • 2017/11/14

    There are few ways how content pages can access elements of master page. you can create public method on master page and call it from content page.

  12. Bryan

    • 2021/7/21

    To do so, go to the Content page (Default.aspx) and add the following line below the <Page> directive to register the MasterPage. <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>.

  13. Gabriel

    • 2015/2/20

    You can do this using Delegates.For Example, you have a button in MasterPage and you want to call a Method in Content Page from Master Page.

  14. Andres

    • 2015/10/13

    1. Create a JavaScript function on the fly and call the JavaScript function in the MasterPage Page_Load () event. someScript = "<script language='javascript'>alert ('Called from CodeBehind');</script>"; Page.ClientScript.RegisterStartupScript ( this .GetType (), "onload", someScript);

  15. Thaddeus

    • 2019/8/17

    It is necessary to call content Page function from Master Page. Please let me know if more data needed. MasterPage.master.cs looks like

  16. Fournier

    • 2017/9/15

    To access master page control name we can write the Javascript in the master page which can be accessed by the content pages. Or in the second work arround if 

Comments are closed.

Recent Posts