Javascript 从asp.net中的contentpage调用masterpage函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12782928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:15:04  来源:igfitidea点击:

Call masterpage function from contentpage in asp.net?

javascriptasp.netmaster-pages

提问by Mennan

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”函数在 masterpage 中声明,所以这段代码不起作用。

setStatusBarMessage is a client side function.

setStatusBarMessage 是一个客户端函数。

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.

此母版页和内容页位于 iframe 对象下。

回答by Ram Singh

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

回答by iJade

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.

如果您尝试在母版页上调用客户端函数,我想您可以直接调用它,因为您的母版页和内容页函数将在同一个 html 页面上呈现。

回答by levi

Usually I do it so: In the markup:

通常我这样做:在标记中:

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


In the code behind:


在后面的代码中:

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

回答by Kapil Arora

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

它一定会帮助你。尝试一下

回答by user3620709

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

标签 lbl = (Label)this.Master.FindControl("lblBalance"); lbl.Text = "你好";