Asp.Net SiteMapPath parent page parameter in querystring or change title

I notice now that I haven’t ever talk about SiteMapPath.
First of all SiteMapPath is a asp.net control very easy to use to obtain breadcrumb navigation.

What we need?
With Visual Studio add new “Site Map” (.sitemap) file to the root project.
Then create the structure of your site like this:

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
  <siteMapNode url="" title="Mysite"  description="">
    <siteMapNode url="~/admin/ManageProjects.aspx" title="Manage projects"  description="">
      <siteMapNode url="~/admin/ProjectDetail.aspx" title="Project detail"  description="" >
        <siteMapNode url="~/admin/AddInfoToProject.aspx" title="Add info to Project"  description="" />
      </siteMapNode>
      <siteMapNode url="~/admin/ImportProjects.aspx" title="Importa progetti"  description="" />
    </siteMapNode>
  </siteMapNode>
</siteMap>

Ok now we have to add the control SiteMapPath in our aspx.page

<asp:SiteMapPath ID="SiteMapPath1" runat="server" CssClass="bread-crumb"></asp:SiteMapPath>

There are 3 usual question using SiteMapPath:

How to hide root node?

RootNodeTemplate Solution (best solution):

<asp:SiteMapPath ID="SiteMapPath1" runat="server" CssClass="bread-crumb">
   <RootNodeTemplate></RootNodeTemplate>
</asp:SiteMapPath>

CSS solution:

/* hide the root */
.bread-crumb span:first-of-type, .bread-crumb span:nth-of-type(2) {
    display: none;
}

How can I change the title with the current item title?

How can I add a query string id to the parent node?

If you try this line in the page it will not work:

SiteMap.CurrentNode.Title = "Item title"

The answer for both questions is manage the SiteMap in global.asax.
So create a global.asax file and write this:

void Application_Start(object sender, EventArgs e) 
{
    // Code that runs on application startup
    SiteMap.SiteMapResolve += SiteMap_SiteMapResolve;
}

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

    if (SiteMap.CurrentNode.Url.Contains("/admin/AddInfoToProject.aspx"))
    {
        // perform a query to sql to get item Title and put it in the title 
        //tempNode.ParentNode.Title = "Item title";
        tempNode.ParentNode.Url += "?id=" + e.Context.Request["Id"];
    }

    return currentNode;
}

When I navigate to the page AddInfoToProject.aspx, the navigation path will be:

Mysite > Manage projects > Project detail > Add info to Project

and the link:
~ > ~/admin/ManageProjects.aspx > ~/admin/ProjectDetail.aspx > ~/admin/AddInfoToProject.aspx (not clicable)

The page ProjectDetail and AddInfoToProject will be called with the Id in query string:

http://localhost:1234/admin/AddInfoToProject.aspx?id=4
http://localhost:1234/admin/ProjectDetail.aspx?id=4

What I’ve done in in the previous code is check if the current node / page is AddInfoToProject.aspx get the Id from the querystring
Concatenate the url of the parent node (“~/admin/ProjectDetail.aspx”) with the id to obtain ~/admin/ProjectDetail.aspx?id=4

So the link of navigation path will be
~ > ~/admin/ManageProjects.aspx > ~/admin/ProjectDetail.aspx?id=4 > ~/admin/AddInfoToProject.aspx (not clicable)

Annunci

Informazioni su Andrea Regoli

Project Manager .Net Developer WPF WP7 Asp.Net c# javascript ajax SQL sharepoint
Questa voce è stata pubblicata in .Net, Asp.Net, c# e contrassegnata con , , , , , , . Contrassegna il permalink.

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...