Preserving query string params in parent breadcrum

2019-08-09 23:14发布

问题:

I'm looking for a way to preserve query string parameters when navigating back up the stack using the standard <asp:SiteMapPath> implementation.

For simplicity, suppose I have a Animal > Breed page stack:

 <siteMapNode url="~/Animal.aspx" title="Animal" >
    <siteMapNode url="~/Breed.aspx" title="Breed"/>  
</siteMapNode>

I navigate to Animal.aspx?type=Dog and select a dog breed to navigate to Breed.aspx?type=Bulldog

On Breed.aspx I have the correct breadcrumb Animal > Breed but the Animal url is just Animal.aspx. How can I get the Animal link to be the parent/previous page Animal.aspx?type=Dog?

In this simplistic example I realise I could feasibly add all the animal type as separate nodes with their relevant type. My actual setup is more complicated and requires ids to be read from the a database.

回答1:

You can attach the parameter to whatever link the user is to click on to navigate...

So you would use something like

mytype = Request.QueryString["type"];

to get the value, the apply that to the link...

mylink.NavigateURL = "animal.aspx?type="+mytype;

I did this outside of the editor, so you'll need to watch for syntax errors...

Here's some documentation...

http://msdn.microsoft.com/en-us/library/ms524784(v=vs.90).aspx



回答2:

As accepted answer here I have added the follow to the detail page (Breed.aspx in the example)

     protected void Page_Load(object sender, EventArgs e){
            if (!IsPostBack)
                SiteMap.SiteMapResolve += new SiteMapResolveEventHandler(SiteMap_SiteMapResolve);
     }

     SiteMapNode SiteMap_SiteMapResolve(object sender, SiteMapResolveEventArgs e){
            SiteMapNode currentNode = SiteMap.CurrentNode.Clone(true);

            currentNode.ParentNode.Url += "?type=" + this.Breed.AnimalType;

            return currentNode;
        }

EDIT

Seems to work well

The url is updated however I use CurrentNode a lot in my menu controls navigating up and down the site map. When I clone I lose sibling information breaking my menus.

I either need to find a way of cloning which preserves the entire sitemap or find another way of amending the ParentNode.Url