简体   繁体   English

当按钮位于更新面板内时,如何从内容页面调用母版页方法?

[英]How do you call master page methods from a content page when the button is inside an update panel?

I have a masterpage and a content page.我有一个母版页和一个内容页。 In the content page I have a script manager and an update panel.在内容页面中,我有一个脚本管理器和一个更新面板。 In the update panel I want to be able to click a button which would hit a public method on the master page to show a message.在更新面板中,我希望能够单击一个按钮,该按钮会点击母版页上的公共方法以显示消息。 This works if I don't have an update panel on the content page but is there a way to get it to work when the button is in an update panel?如果我在内容页面上没有更新面板,这会起作用,但是当按钮位于更新面板中时,有没有办法让它工作?

Master Page:母版页:

public void ShowMessage(string Message) 
{
    lblError.Text = Message; 
    lblError.Visible = True; 
}

Content Page:内容页面:

Master.ShowMessage("something");

I think it's a bit late , but for those who are looking for the solution,我认为现在有点晚了,但对于那些正在寻找解决方案的人来说,

Assuming your master page class like:假设您的母版页类如下:

public MyMAsterPage: MasterPage
{
    public void ShowMessage(string Message) 
    {
       // DO SOMETHING
    }
}

from your content page you can easily call any public method as follow:从您的内容页面,您可以轻松调用任何公共方法,如下所示:

(this.Master as MyMasterPage).ShowMessage("Some argument");

Function which define in masterpage:在 masterpage 中定义的函数:

public void Mesaj(string msj)
{
        lbl_Mesaj.Text = msj;
}

Function which define in content page在内容页面中定义的功能

protected void Page_Load(object sender, EventArgs e)
{
    MasterPageWeb master = (MasterPageWeb)this.Master;
    master.Mesaj("www.zafercomert.com");
}

You can call masterpage's function from content page like this.您可以像这样从内容页面调用 masterpage 的功能。

我最终只是把它搞砸了,把脚本管理器放在母版页上,把标签放在更新面板内的母版页上。

Based on Amin's solution, i used an extension method to solve this more often.基于 Amin 的解决方案,我使用了一种扩展方法来更频繁地解决这个问题。

public static T GetMasterPageObject<T>(this MasterPage masterPage) where T : MasterPage
{
    return (T)masterPage;
}

Example:示例:

this.Master.GetMasterPageObject<MasterPageClass>().MethodYouNeed("Parameters");

You'll need to你需要

this.button1 = this.Master.FindControl("UpdatePanel1").FindControl("Button1") as Button;

Here's a blog post to help describe the process.这是一篇有助于描述该过程的博客文章 Basically you're drilling down into your controls.基本上,您正在深入了解您的控件。

EDIT编辑

Sorry I just re-read your question.抱歉,我刚刚重新阅读了您的问题。 The code above will allow you to FIND a Button on your Masterpage from your Content Page codebehind.上面的代码将允许您从您的内容页面代码隐藏中在您的主页上找到一个按钮。

It's a little more difficult to execute the Masterpage codebehind method from your content page.从内容页面执行 Masterpage 代码隐藏方法要困难一些。 A better way might be to a JavaScript event to your button (or just use jQuery), and put the code for a JavaScript modal window in your Masterpage.更好的方法可能是将 JavaScript 事件添加到您的按钮(或仅使用 jQuery),并将 JavaScript 模式窗口的代码放在您的母版页中。

<!-- script stuff -->
    <script>
    $(function() {
        $( "#dialog" ).dialog({
            autoOpen: false
        });

        $( "#opener" ).click(function() {
            $( "#dialog" ).dialog( "open" );
            return false;
        });
    });
    </script>
<!-- end script stuff -->

<!-- dialog div -->
    <div id="dialog" title="Basic dialog">
        <p>say something meaningful</p>
    </div>
<!-- end dialog div -->

<!-- content page stuff -->
    <button id="opener">open alert</button>
<!-- end content page stuff-->

HOWEVER然而

If you're really hooked on calling a masterpage method from your content page, you need to make the method public如果您真的很喜欢从内容页面调用母版页方法,则需要将该方法设为公开

Find info in Step 4: Calling the Master Page's Public Members from a Content Page步骤 4:从内容页面调用母版页的公共成员中查找信息

There are two ways that a content page can programmatically interface with its master page:内容页面可以通过两种方式与其母版页进行编程交互:

  • Using the Page.Master property, which returns a loosely-typed reference to the master page, or使用 Page.Master 属性,该属性返回对母版页的松散类型引用,或
  • Specify the page's master page type or file path via a @MasterType directive;通过@MasterType 指令指定页面的母版页类型或文件路径; this automatically adds a strongly-typed property to the page named Master.这会自动向名为 Master 的页面添加强类型属性。

<%@ MasterType VirtualPath="~/masters/SourcePage.master" %> <%@ MasterType VirtualPath="~/masters/SourcePage.master" %>

Master.Method(); Master.Method(); (in code behind) (在后面的代码中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM