dynamic asp:menu rendering

  • Thread starter Thread starter tutor
  • Start date Start date
T

tutor

hi folks

i want complete control of my menuing in ASP (no site maps, no hardwired XML
files, etc.) This is all governed by SiteID, ApplicationID, UserID, etc.

i thought i was close with this, but nothing is rending in the control/on
the page (even though I see my 12 items in the databindings are there
menuName.databindings.item(0) - (11)).

(Personally, I don't understand why this is so difficult) Here's my code:

Public Sub BuildMenus(ByVal connString As String, ByVal menuName As
System.Web.UI.WebControls.Menu, ByVal storedProcedure As String, ByVal
idFieldName As String, ByVal fieldName As String, ByVal sortOrder As String, _
ByVal orderBy As String, ByVal whereClause As String, ByVal MyPage
As System.Web.UI.Page, ByVal siteId As Integer, ByVal applicationId As
String, ByVal userId As Integer)

Dim StoreErrorMessage As New SystemTools.StoreErrorMessages(MyPage)
Dim OpenConnection As New IO.OpenConnection

Dim dr As SqlDataReader = Nothing
Dim sqlConn As SqlConnection =
OpenConnection.openConnection(connString, MyPage)

If Not sqlConn Is Nothing Then

Dim sqlCmd As SqlCommand = New
SqlClient.SqlCommand(storedProcedure, sqlConn)

sqlCmd.CommandType = CommandType.StoredProcedure
sqlCmd.Parameters.AddWithValue("@SiteID", siteId)
sqlCmd.Parameters.AddWithValue("@UserID", userId)
sqlCmd.Parameters.AddWithValue("@SortOrder", sortOrder)
sqlCmd.Parameters.AddWithValue("@OrderBy", orderBy)
sqlCmd.Parameters.AddWithValue("@WhereClause", whereClause)

Try
dr = sqlCmd.ExecuteReader(CommandBehavior.CloseConnection)
Catch ex As Exception
DispMessage("Determine Menus Error - " & ex.Message, MyPage)
StoreErrorMessage.StoreSystemErrorMessage(connString,
applicationId, "", "BuildMenus", "GenericRoutines.vb", ex.Message, "")
End Try
End If

If Not dr Is Nothing Then
' [rak] Dim MenuDoc As XmlDocument
While dr.Read

menuName.DataBindings.Add(BuildSimpleMenuBinding(dr.Item("SortID"),
dr.Item("HLevel"), dr.Item("MenuName"), Convert.ToString(dr.Item("Url"))))
'[rak] BindMenuToXmlDocument(MenuDoc, menuName, "menu/item")
End While

menuName.databind()
dr.Close()
End If
End Sub
Public Function BuildSimpleMenuBinding(ByVal XmlElementName As String,
ByVal Depth As Integer, ByVal TextField As String, ByVal NavigationUrlField
As String) As MenuItemBinding

Dim MenuBinding As MenuItemBinding = New MenuItemBinding
MenuBinding.DataMember = XmlElementName
MenuBinding.TextField = TextField
MenuBinding.NavigateUrlField = NavigationUrlField
MenuBinding.Depth = Depth

Return MenuBinding
End Function
Public Sub BindMenuToXmlDocument(ByVal MenuDocument As XmlDocument,
ByRef MenuControl As System.Web.UI.WebControls.Menu, ByVal XPath As String)

Dim MenuDataSource As XmlDataSource = New XmlDataSource()
MenuDataSource.Data = MenuDocument.OuterXml
MenuDataSource.EnableCaching = False
MenuDataSource.XPath = XPath
MenuControl.DataSource = MenuDataSource
MenuControl.DataBind()
End Sub

thanks
rob
 
Consider trying a custom sitemap provider that looks at the database instead
of a site map file. You can then control things however you desire.

As for the particular issues, I would set some breakpoints in the method
that binds and make sure you are actually grabbing the XML document. If you
are, then you have to dig deeper. If you are using Visual Studio 2008, you
can actually debug into the .NET assemblies and see what is going on there.
View source may also give you some hints.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

*************************************************
| Think outside the box!
|
*************************************************
tutor said:
hi folks

i want complete control of my menuing in ASP (no site maps, no hardwired
XML
files, etc.) This is all governed by SiteID, ApplicationID, UserID, etc.

i thought i was close with this, but nothing is rending in the control/on
the page (even though I see my 12 items in the databindings are there
menuName.databindings.item(0) - (11)).

(Personally, I don't understand why this is so difficult) Here's my code:

Public Sub BuildMenus(ByVal connString As String, ByVal menuName As
System.Web.UI.WebControls.Menu, ByVal storedProcedure As String, ByVal
idFieldName As String, ByVal fieldName As String, ByVal sortOrder As
String, _
ByVal orderBy As String, ByVal whereClause As String, ByVal MyPage
As System.Web.UI.Page, ByVal siteId As Integer, ByVal applicationId As
String, ByVal userId As Integer)

Dim StoreErrorMessage As New SystemTools.StoreErrorMessages(MyPage)
Dim OpenConnection As New IO.OpenConnection

Dim dr As SqlDataReader = Nothing
Dim sqlConn As SqlConnection =
OpenConnection.openConnection(connString, MyPage)

If Not sqlConn Is Nothing Then

Dim sqlCmd As SqlCommand = New
SqlClient.SqlCommand(storedProcedure, sqlConn)

sqlCmd.CommandType = CommandType.StoredProcedure
sqlCmd.Parameters.AddWithValue("@SiteID", siteId)
sqlCmd.Parameters.AddWithValue("@UserID", userId)
sqlCmd.Parameters.AddWithValue("@SortOrder", sortOrder)
sqlCmd.Parameters.AddWithValue("@OrderBy", orderBy)
sqlCmd.Parameters.AddWithValue("@WhereClause", whereClause)

Try
dr = sqlCmd.ExecuteReader(CommandBehavior.CloseConnection)
Catch ex As Exception
DispMessage("Determine Menus Error - " & ex.Message,
MyPage)
StoreErrorMessage.StoreSystemErrorMessage(connString,
applicationId, "", "BuildMenus", "GenericRoutines.vb", ex.Message, "")
End Try
End If

If Not dr Is Nothing Then
' [rak] Dim MenuDoc As XmlDocument
While dr.Read

menuName.DataBindings.Add(BuildSimpleMenuBinding(dr.Item("SortID"),
dr.Item("HLevel"), dr.Item("MenuName"), Convert.ToString(dr.Item("Url"))))
'[rak] BindMenuToXmlDocument(MenuDoc, menuName,
"menu/item")
End While

menuName.databind()
dr.Close()
End If
End Sub
Public Function BuildSimpleMenuBinding(ByVal XmlElementName As String,
ByVal Depth As Integer, ByVal TextField As String, ByVal
NavigationUrlField
As String) As MenuItemBinding

Dim MenuBinding As MenuItemBinding = New MenuItemBinding
MenuBinding.DataMember = XmlElementName
MenuBinding.TextField = TextField
MenuBinding.NavigateUrlField = NavigationUrlField
MenuBinding.Depth = Depth

Return MenuBinding
End Function
Public Sub BindMenuToXmlDocument(ByVal MenuDocument As XmlDocument,
ByRef MenuControl As System.Web.UI.WebControls.Menu, ByVal XPath As
String)

Dim MenuDataSource As XmlDataSource = New XmlDataSource()
MenuDataSource.Data = MenuDocument.OuterXml
MenuDataSource.EnableCaching = False
MenuDataSource.XPath = XPath
MenuControl.DataSource = MenuDataSource
MenuControl.DataBind()
End Sub

thanks
rob
 
Back
Top