Call masterpage function from contentpage in asp.net?

百般思念 提交于 2019-12-23 16:10:03

问题


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.


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



回答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.




回答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




回答4:


Usually I do it so: In the markup:

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


In the code behind:

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



回答5:


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



来源:https://stackoverflow.com/questions/12782928/call-masterpage-function-from-contentpage-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!