Mennan
Mennan

Reputation: 4497

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.

Upvotes: 4

Views: 25366

Answers (5)

user3620709
user3620709

Reputation: 1

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

Upvotes: -2

Ram Singh
Ram Singh

Reputation: 6938

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);
}

Upvotes: 5

levi
levi

Reputation: 3521

Usually I do it so: In the markup:

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


In the code behind:

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

Upvotes: 0

Kapil Arora
Kapil Arora

Reputation: 57

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

Upvotes: 0

iJade
iJade

Reputation: 23811

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.

Upvotes: 2

Related Questions