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

发布于 2024-09-28 15:20:19 字数 348 浏览 1 评论 0原文

我有一个母版页和一个内容页。在内容页面中,我有一个脚本管理器和一个更新面板。在更新面板中,我希望能够单击一个按钮,该按钮将点击母版页上的公共方法以显示消息。如果我的内容页面上没有更新面板,则此方法有效,但是当按钮位于更新面板中时,有没有办法让它工作?

母版页:

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

内容页:

Master.ShowMessage("something");

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

慵挽 2024-10-05 15:20:19

我认为这有点晚了,但是对于那些正在寻找解决方案的人来说,

假设您的母版页类如下:

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

从您的内容页面您可以轻松地调用任何公共方法,如下所示:

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

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");
简美 2024-10-05 15:20:19

母版页中定义的函数:

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

内容页中定义的函数

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

您可以像这样从内容页中调用母版页的函数。

Function which define in 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.

澜川若宁 2024-10-05 15:20:19

我最终只是接受了它,将脚本管理器放在主页上,并将标签放在更新面板内的主页上。

I ended up just sucking it up and putting the script manager on the master page and putting the label on the master page inside an update panel.

做个少女永远怀春 2024-10-05 15:20:19

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

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

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

Master.Method(); (in code behind)

执手闯天涯 2024-10-05 15:20:19

您需要使用

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

博客文章来帮助描述该过程。基本上,您正在深入了解您的控件。

编辑

抱歉,我刚刚重新阅读了您的问题。上面的代码将允许您从内容页代码隐藏中查找母版页上的按钮。

从内容页面执行母版页代码隐藏方法有点困难。更好的方法可能是将 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-->

但是,

如果您确实沉迷于从内容页调用母版页方法,则需要将该方法公开

步骤 4:从内容页调用母版页的公共成员

内容页可以通过两种方式与其母版页进行编程交互:

  • 使用 Page.Master 属性,该属性返回对母版页的松散类型引用,或者
  • 通过 @MasterType 指令指定页面的母版页类型或文件路径;这会自动向名为 Master 的页面添加一个强类型属性。

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

<!-- 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

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
  • Specify the page's master page type or file path via a @MasterType directive; this automatically adds a strongly-typed property to the page named Master.
情归归情 2024-10-05 15:20:19

基于Amin的解决方案,我更经常使用扩展方法来解决这个问题。

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

例子:

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

Based on Amin's solution, i used an extension method to solve this more often.

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

Example:

this.Master.GetMasterPageObject<MasterPageClass>().MethodYouNeed("Parameters");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文