1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Custom Menu implemented as a User Control

Custom Menu implemented as a User Control

11 posts, 0 answered
  1. Nitin
    Nitin avatar
    51 posts
    Registered:
    14 Aug 2007
    12 Sep 2007
    Link to this post
    Hi.

    I am working on a user control that will contain web site navigation. Based on the current page, a user is browsing, the user control needs to update its CSS styles etc. 

    Can you please provide sample code to access the current page, all pages ?

    Thanks!

    Nitin
  2. bnye
    bnye avatar
    332 posts
    Registered:
    22 Sep 2005
    12 Sep 2007
    Link to this post

    You could do something like this:

    Dim currentNode1 As SiteMapNode = SiteMap.CurrentNode  
     
                Select Case currentNode1.Url  
                    Case "~/home.aspx" 
                        Me.RadMenu1.Skin = "MenuHome" 
                    Case "~/company.aspx" 
                        Me.RadMenu1.Skin = "MenuCompany" 
                End Select 
     

    Assumes you already have the SkinsPath set in the markup.

    Hope that helps.

    Sincerely,

    Ben
  3. bnye
    bnye avatar
    332 posts
    Registered:
    22 Sep 2005
    12 Sep 2007
    Link to this post
    I forgot to mention that if you wanted the top level node and not just the current node (in case you have a different styles for different top level page groups rather than for every single page) you could do something like this:

    Dim currentNode1 As SiteMapNode = SiteMap.CurrentNode  
     
                If Not currentNode1 Is Nothing Then 
                    While Not currentNode1 Is SiteMap.RootNode  
                        parentNode1 = currentNode1  
                        currentNode1 = currentNode1.ParentNode  
                    End While 
                End If 
     
                If Not parentNode1 Is Nothing Then 
     
                    Select Case parentNode1.Url  
                        Case "~/home.aspx" 
                            Me.RadMenu1.Skin = "MenuHome" 
                        Case "~/company.aspx" 
                            Me.RadMenu1.Skin = "MenuCompany" 
                    End Select 
     
                End If 
     

    Sincerely,

    Ben
  4. Nitin
    Nitin avatar
    51 posts
    Registered:
    14 Aug 2007
    13 Sep 2007
    Link to this post
    Bnye,

    Thanks for the reply. I did end up taking ideas from the posted code. I have got another issue now, however:

    When I try to edit the template (created from the master page with Menu user control on it), I get an exception being generated from my custom code in the Menu user control. Here is my code:

    1     private SiteMapNode GetTopMostParentNodeForCurrentNode()  
    2     {  
    3         SiteMapNode node = SiteMap.CurrentNode;  
    4         while (node.ParentNode != null && node.ParentNode != SiteMap.RootNode)  
    5         {  
    6             node = node.ParentNode;  
    7         }  
    8         return node;  
    9     } 

    SiteMap.CurrentNode on line 3 above is null. I am assuming that my code is getting executed when I try to edit the template. Is there a way to suppress execution of the custom code while using Sitefinity interface?

    Something like - if (! designMode) { // execute code }

    Thanks
  5. bnye
    bnye avatar
    332 posts
    Registered:
    22 Sep 2005
    13 Sep 2007
    Link to this post
    Not sure exactly what the error is...is it just a null reference?

    Did you add the Usercontrol with the menu to the web.config and then use the Template editor to add it to the template? Or did you just add it directly to the template markup from Visual Studio?

    Are you trying to specify your own sitemap datasource?

    Were you able to get the SiteMap.CurrentNode  to work in any situations?

    Sincerely,

    Ben
  6. bnye
    bnye avatar
    332 posts
    Registered:
    22 Sep 2005
    13 Sep 2007
    Link to this post

    woops...I forgot to answer your question. Yes.

    Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load  
            Try 
                editModeCheck()  
                 
            Catch ex As Exception  
     
            End Try 
        End Sub 
        Private Sub editModeCheck()  
            Try 
                Dim editMode As String = String.Empty  
     
                editMode = Request.QueryString("cmspagemode")  
                If Not editMode Is Nothing Then 
                    If editMode.ToUpper = "edit".ToUpper Then 
                        disableControl()  
                    End If 
                End If 
            Catch ex As Exception  
     
            End Try 
        End Sub 
        Private Sub disableControl()  
            Try              
     
            Catch ex As Exception  
     
            End Try 
        End Sub 
  7. Nitin
    Nitin avatar
    51 posts
    Registered:
    14 Aug 2007
    13 Sep 2007
    Link to this post

    Ben,

    The error is Null Reference Exception.

    Here is step  by step description to your question:

    1. Create Menu user control using Visual Studio.
    2. Create the Master page in Visual Studio with menu user control placed in the master page.
    3. Upload the user control using Sitefinity interface.
    4. Upload the Master page using Sitefinity interface.
    5. Create Pages based on the template that Sitefinity creates based on the Master page.


    I am not specifying my own data source.

    Hope that answers your question.

    Thanks

  8. bnye
    bnye avatar
    332 posts
    Registered:
    22 Sep 2005
    13 Sep 2007
    Link to this post
    Nitin -

    I'm not sure what's going that is throwing an error when you edit the template. I suspect that you are trying to check a property of a node before checking to be sure that the node is not null.

    I'm curious if the edit mode check works in the template edit mode. Please let me know.

    Sincerely,

    Ben
  9. Nitin
    Nitin avatar
    51 posts
    Registered:
    14 Aug 2007
    13 Sep 2007
    Link to this post
    Great. Thanks for the reply. Here is how I ended up fixing the issue:

    Create a private property in my menu user control:

        #region Private Properties  
        private bool IsDesignMode  
        {  
            get  
            {  
                bool designMode = false;  
                string pageMode = base.Request.QueryString["cmspagemode"];  
                if (!string.IsNullOrEmpty(pageMode) && (pageMode == "edit" || pageMode == "preview"))  
                {  
                    designMode = true;  
                }  
     
                return designMode;  
            }  
        }  
        #endregion 


    And then calling it in the Page_Load Method of the user control:

            if (!base.IsPostBack && !this.IsDesignMode)  
            {  
                // set menu styles here!  
            }  
     

    Works great. Thanks again!

    Nitin
  10. Sean
    Sean avatar
    271 posts
    Registered:
    31 May 2006
    17 Sep 2007
    Link to this post
    Hi guys,

    Another way to stop this error is by checking if the Page object is null eg

    if (Page !=null ){ 
    // execute your code here 

    This should cover all design scenarios like if you use the user control in a master page too.

    Cheers
    Sean
  11. Nitin
    Nitin avatar
    51 posts
    Registered:
    14 Aug 2007
    17 Sep 2007
    Link to this post
    That looks a lot simpler. I will try it out. Anything for simplicity :)

    Thanks!

    Nitin
Register for webinar
11 posts, 0 answered