Monday, March 26, 2012

Trigger full postback from the server during a partial postback.

Well, I have a dialog user control (ajax popup) which can again contain other user asp user controls as content. The content of the dialog is created inside an UpdatePanel, so that the dialog content can postback. All of that works OK.

On the web page I have now some information visible inside a GridView which is not in an UpdatePanel and should not be, because I need the browser forward/backward button to work. A link on that web page lets one of these dialog controls pop up. The dialog contains a user control which can manipulate the data which is visible inside the GridView. If the user presses a button inside the dialog, the dialog does an asynchronous postback as the user control/dialog content is inside an update panel and the dialog closes. That gives the user the impression that nothing happened as the data visible in the GridView did not change. I do already have the code to refresh the gridview, but how can I change the behavior during the postback from being an asynchronous postback to a full postback?

So just to clarify some pseudo code:

In the user control contained inside the dialog I would have some code like:

void OnOK(...)
{
// check if any data changed
if (textBox.Text != record.Text)
{
// do a database update
// and
// update cached data which the datagrid is bound to
// call a method on the page to rebind the datagrid only

// ? Force a full page refresh
}
else
{
// do nothing
// asynchronous postback is OK, no full page refresh needed
}
}

The force of a full page refresh is what I would need. In a fat environment like Windows Forms, I would just call Invalidate on the Form. Or IF the DataGrid would be inside an UpdatePanel I would search for it and call Update() on it, but then I would loose the browser caching during back and forward movements. So I was searching through all of the classes if there is something like ScriptManager.UpdatePage or something similar, but have found nothing so far.

So what is the solution here? How can inform the ajax client side script, that it should just go ahead and rerender the whole page instead of just extracting the update panel portions?

Thanks

In the <Triggers> collection of your updatepanel, just add whatever control you want to cause a full refresh as a PostBackTrigger instead of as a AsynchPostBackTrigger.

<asp:UpdatePanel ...>
<Triggers>
<asp:PostBackTrigger ControlID="whateverID" />
</Triggers>
<ContentTemplate>
...
</ContentTemplate>
</asp:UpdatePanel>


Well, that does not work:

a) the UserControl is designed separatly and does not have an update panel and vice versa the dialog is a generic control which just adds a UserControl as a content, so it cannot hardcode if there is any control ID inside which should trigger a full postback

b) I do not want a full postback to occur all the time when the button is pressed, the logic of the button on the server side should determine if a full postback is necessary or not, if not then a partial postback is OK.


I tried to work around now, kind of using your approach by doing this in the OnPreRender or OnChildControlsCreated of the UserControl:

System.Web.UI.WebControls.WebControl btnOK =this.Parent.Parent.FindControl("btnOK")as System.Web.UI.WebControls.WebControl;
if (btnOK !=null)
{
System.Web.UI.Control control = btnOK;
UpdatePanel updatePanel = control.ParentasUpdatePanel;
while ((updatePanel ==null) && (control !=null))
{
control = control.Parent;
updatePanel = controlasUpdatePanel;
}
if (updatePanel !=null)
{
PostBackTrigger pbTrigger =newPostBackTrigger();
pbTrigger.ControlID ="btnOK";
updatePanel.Triggers.Add(pbTrigger);
}
}

So I do find the OK button and I do find the UpdatePanel that contains the button, but it has no effect whatsoever.
The control ID must be right, otherwise I would not be able to find the button with it, but still no PostBack and the Dialog itself is not inside an UpdatePanel.

No comments:

Post a Comment