Tuesday, 4 November 2008

ScriptManager histroy tracking with master pages

I was working on a project where I wanted to use the new ASP.NET 3.5 AJAX History control. I started out by viewing the video on http://www.asp.net/ and looking at the source I replicated it in a sample project and it all worked fine.

I introduced a master page file and the history function did not work again.

The main reason is that the scriptmanager.navigate event was not firing.

To overcome this issue I did the following.

Partial Class TestPage
Inherits System.Web.UI.Page

Dim
WithEvents sm As ScriptManager
'Allows us to
set the Scriptmanage
object
for use later

Protected Sub Page_InitComplete(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles
Me.InitComplete
'Set sm with the
current
scriptmanager that
is
on the master
page
sm =
ScriptManager.GetCurrent(Me.Master.Page)
End Sub

Protected Sub
Page_Load(ByVal sender As Object, ByVal e
As
System.EventArgs) Handles
Me.Load
If Not IsPostBack Then
'
Add load
logic
End If
End Sub

Protected Sub
sm_History_Change(ByVal
sender As Object, ByVal e As
System.Web.UI.HistoryEventArgs) Handles
sm.Navigate 'Access the
scriptmanager event
referenced as "sm"
If
String.IsNullOrEmpty(e.State("Index")) Then
LoadMainView(0)
Else
LoadMainView(CInt(e.State("Index")))
End If
End Sub
End Class


As you can see from this cut down version of the aspx code behind page I declared "WithEvents" sm as the script manager. I used the Page_InitComplete event to wireup / set the sm object with the current scripmanager object.

The below link to asp.net video archive really helped.
http://www.asp.net/learn/3.5-SP1/video-242.aspx

There are better ways to wireup an objects event but this simply shows one way to accomplish what you desire.

No comments:

Post a Comment

Please enter your comment