Wednesday, September 21, 2011

ScriptManager History UpdatePanel

I just beat my head against the wall for an entire morning trying to figure out why the simple ScriptManager History functionality was not working with my UpdatePanel.

First off, I inherited this code, so I was just working with what was there without paying much attention to it. The previous person had setup an UpdatePanel without putting in a Trigger tag. So that should have been clue NUMBER 1, but I overlooked that so that was a good two hours wasted in just not paying attention to that.

Oh well, so I kept following all the simple steps listed on the 'net for setting up the ScriptManager to keep history points. Brief overview:

1) Add a ScriptManager to the page like this:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnableHistory="True" OnNavigate="MainScriptManager_Navigate" EnableSecureHistoryState="True"/>


2)Add the associated function to the code-behind :
protected void MainScriptManager_Navigate(object sender, HistoryEventArgs e)
{
    string panelName = e.State["S"];

     switch (panelName)
     {
      case "Panel_Store_CC":
      Panel_Store_CC.Visible = true;
      break;
      default:
      break;
     }
}

3) Add history points when needed in code calls (in my example it was a button click):
if (ScriptManager1.IsInAsyncPostBack && !ScriptManager1.IsNavigating)
 ScriptManager.GetCurrent(this).AddHistoryPoint("S", "Panel_Store_CC");


After several hours, history was not being added the browser's back in any browser (Chrome/IE/FireFox)....I began noticing during my debugging I was hitting the Page_Load code on each button click. DUH! This was not right...so I finally figured out that I needed a trigger for the updatepanel. At first I added an Asynchoronous trigger, but that still did not work HOWEVER once I added the PostBackTrigger for the button, it finally worked since the entire page was not being posted back!

<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<Triggers>
<asp:PostBackTrigger ControlID="Button1" />
</Triggers>

No comments:

Post a Comment