Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

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.

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top