Call masterpage function from contentpage in asp.net?

I have a function on masterpage and i want to call it from content page from codebehind.

this is my trying :

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert__", string.Format("setStatusBarMessage('{0}',{1});", barMessage, type, ""), true);

“setStatusBarMessage” function is declare in masterpage , so this code doesnt working.

setStatusBarMessage is a client side function.

MasterPage:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Content.master.cs" 

Inherits="F8.CRM.Pages.Content" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager runat="server" />
    <div>
        <asp:ContentPlaceHolder ID="MainContent" runat="server">
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>
<script type="text/javascript">


    function hello() {
        alert('hi mennan');
    }

</script>

ContentPage :

<%@ Page Title="" Language="C#" MasterPageFile="~/Pages/Content.Master" AutoEventWireup="true"
    CodeBehind="Departman.aspx.cs" Inherits="F8.CRM.Departman" %>

<%@ Register Src="~/Controls/Objects/StudioSideBox/StudioSideBox.ascx" TagName="StudioSideBox"
    TagPrefix="uc1" %>
<%@ Register Src="~/Controls/Objects/Baslik/Baslik.ascx" TagName="Baslik" TagPrefix="uc2" %>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

     my html...

    <script type="text/javascript">

       my script codes...


    </script>

</asp:Content>

This masterpage and content page is under a iframe object.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

ok try the following code

I have a function in Master Page that is

<script>
    function hello() {
        alert('hi');
    }

</script>

Now on content page’ page load

 protected void Page_Load(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(this, this.GetType(), "ntmtch", "hello();", true);
}

It working. i haven’t added any thing to content page.

Update

Master page’s Code

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script>
    function hello() {
        alert('hi');
    }

</script>
<asp:ContentPlaceHolder id="head" runat="server">

</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
    <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

    </asp:ContentPlaceHolder>
</div>
</form>

First Content Page’s code

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<iframe src="Default2.aspx"></iframe>
</asp:Content>

Code behind Of first Content Page:

 protected void Page_Load(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(this, this.GetType(), "ntmtch", "hello();", true);
}

Method 2

Try this for calling master page server side function

    MasterPagename ms = Master as MasterPagename ;
    ms.FuctionOnMasterPage();

If u r trying to call client side function on master page, the i guess u can call it directly since ur master page and content page function will get rendered on the same html page.

Method 3

Here We can Make a Object of Master Page Class and then we can Call MasterPage Function

 MasterPageClassName MyMasterPage = (MasterPageClassName)Page.Master;

 MyMasterPage.Functionname();

It will definitely Help you. Try It

Method 4

Usually I do it so: In the markup:

<asp:Literal ID="ScriptLit" runat="server" />

In the code behind:

ScriptLit.Text = "<script>functionName();</script>"

Method 5

Label lbl = (Label)this.Master.FindControl(“lblBalance”);
lbl.Text = “Hi”;


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x