【问题标题】:Refresh the master page only from the content page仅从内容页面刷新母版页
【发布时间】:2015-06-12 11:54:44
【问题描述】:

我不知道如何通过单击 ContentPlaceHolder 部分中的按钮来(仅)刷新我的母版页。

这是解释情况的图像(我模糊了无用的部分)。

须知:

蓝色:母版页

绿色:我要刷新的 MasterPage 的一部分

红色:MasterPage 的 ContentPlaceHolder(另一个 aspx 页面)

我想用红色部分的Button的Click事件来刷新MasterPage的绿色部分。

我尝试了围绕绿色部分的更新面板,触发器是红色部分按钮的 Click 事件,但由于它不是同一个 aspx 页面,所以它失败了。

有什么想法吗?

编辑:我的代码

母版页 C# 部分:

public void UpdateComment()
        {
            this.UPDP_Obs.Update();
        }

母版页 Asp 部分:

 <asp:UpdatePanel ID="UPDP_Obs" runat="server"
                             UpdateMode="Conditional">
                <ContentTemplate>

                <asp:Table CssClass="center" ID="TBL_ObsLA" runat="server">
                <asp:TableRow runat="server">
                    <asp:TableCell>
                        Dernier commentaire Ligne Cal-1 A:
                    </asp:TableCell>
                </asp:TableRow>
                <asp:TableRow runat="server"> 
                    <asp:TableCell>
                    <asp:Label ID="LBL_DateLA" runat="server" Text=""></asp:Label> <br /> <br />
                    <asp:Label ID="LBL_ObsLA" runat="server" Text=""></asp:Label>
                    </asp:TableCell>
                </asp:TableRow>

            </ContentTemplate>
</asp:UpdatePanel>

ContentPlaceHolder c#部分:

//Some Code to Update the comment in Sql to a Database (works)


MasterLavage MasterP = (MasterLavage)this.Master;
MasterP.UpdateComment();

我使用调试器进行了跟踪,编译通过 UpdateComment 方法进行,但 Updatepanel 未刷新(或至少标签未刷新)。 当我刷新整个页面 (F5) 时,标签会用新评论刷新

有什么想法吗?

【问题讨论】:

  • 我知道这可能是你最不想听到的,但我会打破严格的 WebForms 模型并使用 JavaScript 执行 Ajax 请求以拉入数据,然后更新字段JavaScript。更新面板是如此 2008 年。;-)

标签: javascript c# jquery asp.net


【解决方案1】:

尝试使用(它的 predocode,因为你没有指定语言)

在 ContentPlaceHolder 服务器语言脚本中(例如 .vb、.cs 文件)

this.parent.method_that_updates(); //you can check if method exists before invoking, just to be safe; or wrap this in a try catch block

在母版页中

public function method_that_updates() {
     this.my_panel.refresh(); //or whatever does the refresh
     //notice I made it public
}

【讨论】:

  • 你好,我更新了我的问题并放了一些代码,如果你能看一下请:),谢谢你的想法
  • this.UPDP_Obs.Update(); 不,标签是这样刷新的吗,如果需要从数据库中获取,则需要使用//Some Code to Also Select from the database。添加 this.UPDP_Obs 最初如何从数据库中收集其信息,同样的方式应该在 this.UPDP_Obs.Update(); 当前所在的地方
【解决方案2】:

制作主人的UpdatePanel的UpdateMode=Conditional并从页面手动更新。您可以在您的母版中提供一个公共方法,您可以从您的页面调用该方法。 此方法接受您要更改的信息并调用Update:

// in your Masterpage
public void UpdateComment(string comment)
{
   this.LblComment.Text = comment;    // as label in the green part of the master
   this.CommentUpdatePanel.Update();  // the UpdatePanel around this control
}

您必须将页面的Master 属性转换为正确的类型才能看到UpdateComment。

【讨论】:

  • 您好,我刚刚尝试了您的解决方案,但它不起作用,我认为这个想法很好,我可能用错了,我已经用代码更新了我的线程问题,你能看看?
  • @charlesgodin:我看不到您在代码中更改注释的位置。
  • @charlesgodin:我看到了,但我没有看到你在哪里更改评论。如果没有任何变化,您希望更新什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-04
  • 1970-01-01
  • 1970-01-01
  • 2013-01-25
  • 1970-01-01
  • 1970-01-01
  • 2011-07-04
相关资源
最近更新 更多