locked
Modify Sitemappath control node to include querystring value RRS feed

  • Question

  • User1909155429 posted

    I have been looking at the following doc example but system does not understand the handler methods shown? I want to click on specific node and return a querystring value back to previous page. 

    I am working in VB, not C# as illustrated.

    <asp:SiteMapPath ID="SiteMapPath1" runat="server" CssClass="icon-home" ></asp:SiteMapPath>

    private void Page_Load(object sender, EventArgs e)
    {
    // The ExpandForumPaths method is called to handle 
    // the SiteMapResolve event.
    SiteMap.SiteMapResolve +=
    new SiteMapResolveEventHandler(this.ExpandForumPaths);
    }

    private SiteMapNode ExpandForumPaths(Object sender, SiteMapResolveEventArgs e)
    {
    // The current node represents a Post page in a bulletin board forum. 
    // Clone the current node and all of its relevant parents. This 
    // returns a site map node that a developer can then 
    // walk, modifying each node.Url property in turn. 
    // Since the cloned nodes are separate from the underlying 
    // site navigation structure, the fixups that are made do not 
    // effect the overall site navigation structure.
    SiteMapNode currentNode = SiteMap.CurrentNode.Clone(true);
    SiteMapNode tempNode = currentNode;

    // Obtain the recent IDs. 
    int forumGroupID = GetMostRecentForumGroupID();
    int forumID = GetMostRecentForumID(forumGroupID);
    int postID = GetMostRecentPostID(forumID);

    // The current node, and its parents, can be modified to include 
    // dynamic querystring information relevant to the currently 
    // executing request. 
    if (0 != postID)
    {
    tempNode.Url = tempNode.Url + "?PostID=" + postID.ToString();
    }

    if ((null != (tempNode = tempNode.ParentNode)) &&
    (0 != forumID))
    {
    tempNode.Url = tempNode.Url + "?ForumID=" + forumID.ToString();
    }

    if ((null != (tempNode = tempNode.ParentNode)) &&
    (0 != forumGroupID))
    {
    tempNode.Url = tempNode.Url + "?ForumGroupID=" + forumGroupID.ToString();
    }

    return currentNode;
    }


    ...


    // These methods are just placeholders for the example. 
    // One option is to use the HttpContext or e.Context object 
    // to obtain the ID. 
    private int GetMostRecentForumGroupID()
    {
    return 24;
    }

    private int GetMostRecentForumID(int forumGroupId)
    {
    return 128;
    }

    private int GetMostRecentPostID(int forumId)
    {
    return 317424;
    }

    Tuesday, June 23, 2020 9:36 PM

Answers

  • User288213138 posted

    Hi peterthegreat,

    'Public Shared Event SiteMapResolve(sender As Object, e As System.Web.SiteMapResolveEventArgs) As System.Web.SiteMapNode' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

    I am working in VB, not C# as illustrated.

    I tested your code, it is working in C#, but not working in VB.

    If you want tested this code in vb, you can try this code:

    Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    
        AddHandler SiteMap.SiteMapResolve, AddressOf Me.ExpandForumPaths
    
    End Sub

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 25, 2020 9:38 AM

All replies

  • User288213138 posted

    Hi peterthegreat,

    I have been looking at the following doc example but system does not understand the handler methods shown? I want to click on specific node and return a querystring value back to previous page. 

    I am working in VB, not C# as illustrated

    Is there any error message in C#?

    private void Page_Load(object sender, EventArgs e)
    {
    // The ExpandForumPaths method is called to handle 
    // the SiteMapResolve event.
    SiteMap.SiteMapResolve +=
    new SiteMapResolveEventHandler(this.ExpandForumPaths);
    }

    private SiteMapNode ExpandForumPaths(Object sender, SiteMapResolveEventArgs e)
    {
    // The current node represents a Post page in a bulletin board forum. 
    // Clone the current node and all of its relevant parents. This 
    // returns a site map node that a developer can then 
    // walk, modifying each node.Url property in turn. 
    // Since the cloned nodes are separate from the underlying 
    // site navigation structure, the fixups that are made do not 
    // effect the overall site navigation structure.
    SiteMapNode currentNode = SiteMap.CurrentNode.Clone(true);
    SiteMapNode tempNode = currentNode;

    // Obtain the recent IDs. 
    int forumGroupID = GetMostRecentForumGroupID();
    int forumID = GetMostRecentForumID(forumGroupID);
    int postID = GetMostRecentPostID(forumID);

    // The current node, and its parents, can be modified to include 
    // dynamic querystring information relevant to the currently 
    // executing request. 
    if (0 != postID)
    {
    tempNode.Url = tempNode.Url + "?PostID=" + postID.ToString();
    }

    if ((null != (tempNode = tempNode.ParentNode)) &&
    (0 != forumID))
    {
    tempNode.Url = tempNode.Url + "?ForumID=" + forumID.ToString();
    }

    if ((null != (tempNode = tempNode.ParentNode)) &&
    (0 != forumGroupID))
    {
    tempNode.Url = tempNode.Url + "?ForumGroupID=" + forumGroupID.ToString();
    }

    return currentNode;
    }


    ...


    // These methods are just placeholders for the example. 
    // One option is to use the HttpContext or e.Context object 
    // to obtain the ID. 
    private int GetMostRecentForumGroupID()
    {
    return 24;
    }

    private int GetMostRecentForumID(int forumGroupId)
    {
    return 128;
    }

    private int GetMostRecentPostID(int forumId)
    {
    return 317424;
    }

    This code example demonstrates how to call the Clone method to create a duplicate site map node from the current node. The ExpandForumPaths method is registered to handle the SiteMapResolve event. It uses the Clone method to create a working copy of the current site map node, modify attributes based on personalization data, and return the working copy. and it works fine.

    <asp:SiteMapPath ID="SiteMapPath1" runat="server" CssClass="icon-home" ></asp:SiteMapPath>

    The problem is that you need to create a web.sitemap.

    Best regards,

    Sam

    Wednesday, June 24, 2020 2:48 AM
  • User1909155429 posted

    SiteMap.SiteMapResolve += New SiteMapResolveEventHandler(Me.ExpandForumPaths)

    Source Error:

    'Public Shared Event SiteMapResolve(sender As Object, e As System.Web.SiteMapResolveEventArgs) As System.Web.SiteMapNode' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

    whereas:  ExpandForumPaths when hover over, says requires a Addressof expression as the only argument to its constructor?

    Thanks

    Wednesday, June 24, 2020 7:38 PM
  • User288213138 posted

    Hi peterthegreat,

    'Public Shared Event SiteMapResolve(sender As Object, e As System.Web.SiteMapResolveEventArgs) As System.Web.SiteMapNode' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

    I am working in VB, not C# as illustrated.

    I tested your code, it is working in C#, but not working in VB.

    If you want tested this code in vb, you can try this code:

    Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    
        AddHandler SiteMap.SiteMapResolve, AddressOf Me.ExpandForumPaths
    
    End Sub

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 25, 2020 9:38 AM
  • User1909155429 posted

    That article is misleading to say the least i swopt and changed code to finally get it to work!!smile

    I just had to attach the parent method to the node, whereas that example  shows three  lots of the same sitemapnode properties. It confused me for days on end until finally.

    How is it that the UrlReferrer automatically displays previous page content?

    Thursday, June 25, 2020 7:20 PM
  • User288213138 posted

    Hi peterthegreat,

    I just had to attach the parent method to the node, whereas that example  shows three  lots of the same sitemapnode properties. It confused me for days on end until finally.

    How is it that the UrlReferrer automatically displays previous page content?

    According to your description, I couldn’t understand your requirement clearly. please post more details information about your requirement.

    And this is another question, please post your question in the new thread.

    Best regards,

    Sam

    Friday, June 26, 2020 7:57 AM
  • User1909155429 posted

    I Samwu,

    I tried to apply the same script that had been working fine to another page, but i get constant error messages saying the node collection is read only? I tried a number of remedies yet still obtain the same response. Another thing that tends to occur is the handler does not always catch?

    Thanks

    Sunday, June 28, 2020 12:35 PM
  • User288213138 posted

    Hi peterthegreat,

    I tried to apply the same script that had been working fine to another page, but i get constant error messages saying the node collection is read only? I tried a number of remedies yet still obtain the same response. Another thing that tends to occur is the handler does not always catch?

    According to your description, I cannot reproduce your problem.

    Can you show me your tested code? and this is  a new question, please post your question in new thread.

    Best regards,

    Sam

    Tuesday, June 30, 2020 7:57 AM
  • User1909155429 posted

    Here's the exception message

    System.InvalidOperationException was unhandled by user code
    HResult=-2146233079
    Message=SiteMapNode is readonly, property Url cannot be modified.
    Source=System.Web
    StackTrace:
    at System.Web.SiteMapNode.set_Url(String value)
    at SELECTIMAGES_mnuTabs.EXPANDFORUMPATHS(Object SENDER, SiteMapResolveEventArgs e) in C:\projectNew_Latest\MyProject\ProductDetails.aspx.vb:line 671
    at System.Web.SiteMapProvider.ResolveSiteMapNode(HttpContext context)
    at System.Web.SiteMapProvider.get_CurrentNode()
    at System.Web.XmlSiteMapProvider.get_CurrentNode()
    at System.Web.SiteMapProvider.GetCurrentNodeAndHintAncestorNodes(Int32 upLevel)
    at System.Web.UI.WebControls.SiteMapPath.CreateControlHierarchy()
    at System.Web.UI.WebControls.SiteMapPath.CreateChildControls()
    at System.Web.UI.Control.EnsureChildControls()
    at System.Web.UI.Control.FindControl(String id, Int32 pathOffset)
    at System.Web.UI.Control.FindControl(String id, Int32 pathOffset)
    at System.Web.UI.Page.FindControl(String id)
    at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
    InnerException:

    Code

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

    AddHandler SiteMap.SiteMapResolve, AddressOf Me.EXPANDFORUMPATHS

    End Sub

    Public Function EXPANDFORUMPATHS(SENDER As Object, e As SiteMapResolveEventArgs) As SiteMapNode
    Dim currentNode As SiteMapNode = SiteMap.CurrentNode.Clone(True)
    Dim tempNode As SiteMapNode = currentNode.RootNode
    'Dim forumGroupID As Integer = GetMostRecentForumGroupID()
    'Dim forumID As Integer = GetMostRecentForumID(forumGroupID)
    Dim PostID As Integer = 7

    If PostID <> 0 Then
    tempNode.RootNode.Url = tempNode.RootNode.Url & "?PostID=" & PostID.ToString()
    End If

    Sunday, July 5, 2020 2:34 PM