我在更新面板中有一个按钮,我想更新整个页面。我设置了 ChildrenAsTriggers =" false" UpdateMode =" Conditional"

我在这里有一些示例代码,用于演示我的问题。

<asp:UpdatePanel ID="myFirstPanel" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
   <ContentTemplate>
      <asp:Button runat="server" ID="myFirstButton" Text="My First Button" onclick="myFirstButton_Click" />
      <asp:Button runat="server" ID="mySecondButton" Text="My Second Button" onclick="mySecondButton_Click" />
   </ContentTemplate>
</asp:UpdatePanel>

<asp:UpdatePanel ID="mySecondPanel" runat="server">
   <ContentTemplate>
       <asp:Label runat="server" ID="myFirstLabel" Text="My First Label"></asp:Label>
    </ContentTemplate>
    <Triggers>
       <asp:AsyncPostBackTrigger ControlID="myFirstButton" EventName="Click" />
    </Triggers>
</asp:UpdatePanel>
<asp:Label runat="server" ID="mySecondLabel" Text="My Second Label"></asp:Label>

背后的代码:

protected void myFirstButton_Click(object sender, EventArgs e)
{
  myFirstLabel.Text = "Inside Panel " + DateTime.Now.ToString("mm:ss");
}
protected void mySecondButton_Click(object sender, EventArgs e)
{
  mySecondLabel.Text = "Outside Panel " + DateTime.Now.ToString("mm:ss");
}

我希望在单击第二个按钮时更新不在更新面板内的标签。 第二个按钮需要位于更新面板中。我不想把标签放到更新面板中。

有帮助吗?

解决方案

尝试将PostBack触发器添加到第一个UpdatePanel,第二个按钮。这将告诉更新面板,该按钮应该进行完整的回发。

其他提示

ScriptManager.GetCurrent(页).RegisterPostBackControl(按钮);将解决您的问题

如果你将PostBackTrigger添加到第一个更新面板,那么

<Triggers>
   <asp:PostBackTrigger ControlID="mySecondButton" />
</Triggers>

单击第二个按钮时,这将导致完整的回发。这是我正在寻找的确切行为。

我有一个类似但稍微复杂的案例,我花了一天多的时间来弄清楚这个答案。 (答案基本上与@serkan给出的答案相同。我只是在扩展它。)我们有一个ASP.NET用户控件,它有一个按钮,当点击它时,生成一个图像并触发图像下载。这在普通页面上运行得很漂亮,但在用户控件嵌入更新面板时根本没有。我无法弄清楚如何告诉更新面板仅针对此按钮进行常规回发,特别是因为按钮位于用户控件内,并且使用它无法从页面中明显访问它。

最终起作用的非常简单。无需尝试为面板指定回发触发器。相反,在用户控件的Page_Load中,我添加了以下内容:

    protected void Page_Load(object sender, EventArgs e)
    {
        // If the chart is within an update panel then we need to tell the script manager
        // to not do an asynch postback on chartdownload.  If we don't, the download
        // doesn't work.
        var scriptManager = ScriptManager.GetCurrent(Page);
        if (scriptManager != null)
        {
            scriptManager.RegisterPostBackControl(ChartSaveButton);
        }
        // Rest of Page_Load followed here...

此外,我创建了 ChartSaveButton 属性,以便轻松访问按钮的控件:

    private Control ChartSaveButton
    {
        get { return FindControl("btnDownloadChart"); }
    }

(&quot; btnDownloadChart&quot;是按钮的ID。)

到达这里是一条痛苦的道路,但我很高兴解决方案非常优雅。我希望这会对你有所帮助。

对我来说,这种设置没有意义。请记住,虽然更新面板可以很容易地实现类似ajax的效果,但它们在所有情况下都不是理想的。

如果您的页面架构需要像示例中那样排列元素,那么最好使用javascript来更新标签来使用页面或服务方法。

如果标签不在更新面板中,刷新值的唯一方法是刷新整个页面,就像在正常的回发中一样。您必须重定向到同一页面才能刷新它。即使请求来自更新面板,ajax框架也知道处理重定向,因此只需在按钮单击时执行Response.Redirect(...)。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top