如何获取 asp:UpdatePanel 内的按钮来更新整个页面?

发布于 2024-07-08 16:00:20 字数 1445 浏览 7 评论 0原文

我在更新面板中有一个按钮,我想更新整个页面。 我设置了 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");
}

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

I have a button inside an update panel that I would like to update the whole page. I have set ChildrenAsTriggers="false" and UpdateMode="Conditional".

I have some sample code here that demonstrates my problem.

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

And the code behind:

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

I want to update the label that is not inside an update panel when the second button is clicked.
The second button needs to be in an update panel. I don't want to put the lable into an update panel.

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

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

发布评论

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

评论(6

信仰 2024-07-15 16:00:20

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

Try adding a PostBackTrigger to the first UpdatePanel, for the secound button. That will tell that update panel, that the button should make a full postback.

戴着白色围巾的女孩 2024-07-15 16:00:20

ScriptManager.GetCurrent(Page).RegisterPostBackControl(按钮); 会解决你的问题

ScriptManager.GetCurrent(Page).RegisterPostBackControl(button); will solve your problem

陌路黄昏 2024-07-15 16:00:20

如果您将 PostBackTrigger 添加到第一个更新面板,如下所示,

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

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

If you add a PostBackTrigger to the first update panel like so

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

This will cause a full post back to happen when the second button is clicked. This is the exact behaviour I was looking for.

jJeQQOZ5 2024-07-15 16:00:20

我有一个类似但稍微复杂的案例,我花了一天多的时间才找到这个答案。 (答案本质上与 @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"); }
    }

" btnDownloadChart”是按钮的ID。)

这是一条痛苦的道路,但我很高兴这个解决方案如此优雅。 我希望这能帮到您。

I had a similar but slightly more complex case and it took me more than a day to figure out this answer. (The answer is essentially the same as that given by @serkan. I'm just expanding on it.) We had an ASP.NET user control that had a button which, when clicked, generated an image and triggered a download of the image. This worked beautifully on a normal page, but not at all when the user control was embedded in an update panel. I could not figure out how to tell the update panel to do a regular postback for only this button, especially since the button was within the user control with no obvious access to it from the page using it.

What finally worked was extremely simple. There was no need to try to specify the postback trigger for the panel. Rather, in the Page_Load of the user control, I added the following:

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

In addition, I created the ChartSaveButton property to get easy access to the control for the button:

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

("btnDownloadChart" is the ID of the button.)

It was a painful road to get here, but I am gratified the solution is so elegant. I hope this will help you.

蘸点软妹酱 2024-07-15 16:00:20

对我来说,这个设置没有意义。 请记住,虽然更新面板是一种非常轻松地实现类似 ajax 效果的好方法,但它们并非在所有情况下都是理想的。

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

to me, this setup doesn't make sense. keep in mind that while update panels can be a great way of achieving an ajax like effect very easily, they're not ideal in all situations.

if your page architecture requires the arrangement of elements as you have in your example, you may be better off looking at using page or service methods with javascript to update the label.

迷爱 2024-07-15 16:00:20

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

If the label is not in an update panel the only way to refresh the value is by refreshing the entire page like in a normal postback. You would have to do a redirect to the same page to refresh it. The ajax framework knows to handle the redirects even if the request is from an update panel so just do an Response.Redirect(...) on the button click.

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