Tales from the Evil Empire

Bertrand Le Roy's blog

News


Bertrand Le Roy

BoudinFatal's Gamercard

Tales from the Evil Empire - Blogged

Blogs I read

My other stuff

Archives

Screencast: how to enable server-side history management in an ASP.NET Ajax application

I've recently recorded a screencast showing how to enable server-side history management (in other words, handling the back button) in an ASP.NET Ajax application. The whole video is less than 15 minutes total and I build the whole application from scratch in there (in VB).

I hope it shows just how simple history management is made by ASP.NET Ajax and that it helps understanding the state model that it's built on.

The same video in a variety of formats and in a resolution where the code is actually readable can be found here:
http://www.asp.net/downloads/3.5-extensions/

For those people who find it difficult to cut and paste code from a video by doing a screen capture and then using OCR over it, here's the source code of the mini-app that's being built in the video:

Default.aspx:

<asp:ScriptManager ID="ScriptManager1" runat="server" 
    EnableHistory="True" EnableStateHash="False"/>
<
asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:Wizard ID="Wizard1" runat="server"> <WizardSteps> <asp:WizardStep runat="server" Title="Step 1"> <asp:TextBox ID="TextBox1" runat="server"/> </asp:WizardStep> <asp:WizardStep runat="server" Title="Step 2"> <asp:TextBox ID="TextBox2" runat="server"/> </asp:WizardStep> <asp:WizardStep runat="server" Title="Step 3"> <asp:TextBox ID="TextBox3" runat="server"/> </asp:WizardStep> </WizardSteps> </asp:Wizard> </ContentTemplate> </asp:UpdatePanel>

Default.aspx.vb:

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Wizard1_ActiveStepChanged(_
ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Wizard1.ActiveStepChanged
If ScriptManager1.IsInAsyncPostBack And _
Not ScriptManager1.IsNavigating Then
ScriptManager1.AddHistoryPoint("index", _ Wizard1.ActiveStepIndex, _ "Wizard step " & Wizard1.ActiveStepIndex) End If End Sub Protected Sub ScriptManager1_Navigate(_
ByVal sender As Object, ByVal e As HistoryEventArgs) _
Handles ScriptManager1.Navigate
Dim indexString As String = e.State("index") If String.IsNullOrEmpty(indexString) Then Wizard1.ActiveStepIndex = 0 Else Dim index As Integer = Convert.ToInt32(indexString) Wizard1.ActiveStepIndex = index End If Page.Title = "Wizard step " & indexString End Sub End Class

http://www.asp.net/downloads/3.5-extensions/

Comments

Bertrand Le Roy said:

Zahi: there is a link right under the embedded video that leads to the same video in a larger size.

# January 5, 2008 3:44 PM

alex grach said:

what if the scriptmanager is in a masterpage and the wizard is in a contentpalceholder in a page that uses the masterpage. i have been having an issue with this situation. i am using ScriptManager.GetCurrent(this) in the page to get the scriptmanager in the masterpage and set the properties to enable it to support history, but when i click the back button to go all the way back to the first step of the wizard it thinks there is still one more step backwards and if i click back it throws an event validation error. how can i fix this.

# January 29, 2008 2:44 PM

Bertrand Le Roy said:

Yes, Alex, I saw your other post on the DNS article. Please send me your code (bleroy at microsoft) and I'll look at it.

# January 29, 2008 6:51 PM

veni214 said:

does the "enable history" on script manager exist in asp.net 2.0??

# February 12, 2008 10:15 AM

Bertrand Le Roy said:

No.

# February 12, 2008 3:11 PM

Mehedi said:

Nice. I learned a great thing today.

Thanx

# April 20, 2008 3:01 AM

Tim C said:

So you had to manually change the active step of the wizard. What state gets restored? For example, if in one of your wizard steps you had a button that when clicked updated a label's text property. Then you go forward then clicked back, will the label's text be updated or would it have its original state before the async postback from button?

# May 15, 2008 11:13 AM

Bertrand Le Roy said:

@Tim: you should include in the state any information that you need to rebuild the page's state. In your example, you'd probably store the fact that the button was clicked in addition to the wizard step.

# May 28, 2008 7:41 PM