By
Norty | August 24, 2010 21:01
Problem:
We wanted to use Web.sitemap XML file in ASP.Net to populate our breadcrumb control in our Master Page. The issue with the default functionality of the ASP.Net SiteMapPath tools is that it requires default.aspx to be in the NavigateURL of each link. Functionally that is an acceptable method, but we didn't want to display the default.aspx filename and extension to the user. Removing the filename gives the website cleaner URLs and creates easier links for users and SEO.
Solution:
You have to manipulate the SiteMap object in order to achieve these results. Our project used a Master Page, so in the page load of the master page you need C# code:
protected void Page_Load(object sender, EventArgs e)
{
SiteMap.SiteMapResolve += new
SiteMapResolveEventHandler(SiteMap_SiteMapResolve);
}
SiteMapNode SiteMap_SiteMapResolve(object sender,
SiteMapResolveEventArgs e)
{
SiteMapNode currentNode = SiteMap.CurrentNode.Clone(true);
SiteMapNode tempNode = currentNode;
tempNode = _fixParent(tempNode);
return currentNode;
}
protected SiteMapNode _fixParent(SiteMapNode s)
{
s.Url = _normalizeURL(s.Url);
if (s.ParentNode != null)
{
s.ParentNode = _fixParent(s.ParentNode);
}
return (s);
}
protected string _normalizeURL(string s)
{
if(s.Contains("default.aspx")) {
return(s.Substring(0, s.LastIndexOf("/")));
} else {
return(s);
}
}
This code allows for each page like http://domain.com/category/subcat/ to display the breadcrumb even though it is listed with its complete path and filename (~/category/subcat/default.aspx) in the Web.sitemap file. Looping through the parents fixes link on each breadcrumb to function sans default.aspx as well.
By
Norty | June 11, 2010 09:01
In an aspx page you can't include the runat=”server” attribute for a script file.
Problem:
<script src=”~/scripts/jsfile.js” type=”text/javascript”><script>
Including the runat=”server” attribute will produce a compilation error on the page. A work around to this issue is to use the ResolveUrl() method.
Solution:
<script src=”<%ResolveUrl(”~/scripts/jsfile.js”)%>” type=”text/javascript”></script>
Utilizing the ResolveUrl() method will replace the instance of ~ in the string with the value of the application’s path; thus, providing the same benefit that runat=”server” gives you in style sheet links and image tags.
By
Norty | April 14, 2010 10:35
Using jQuery to output the current year is a useful substitute when your website pages are done in HTML and not with server side languages like PHP or ASP. The following code demonstrates how to print the current year in HTML using jQuery.
First, you will need to reference a jQuery library in the <head> of your HTML. You can do this one of two ways:
- Link directly to the jQuery library on the jQuery code site
<script src=”http://code.jquery.com/jquery-1.4.2.min.js” type=”text/javascript”></script>
- Download a version of jQuery from the jQuery site (http://jquery.com) and relatively link to it from within your site
<script src=”/script/jquery-1.4.2.min.js” type=”text/javascript”></script>
With the jQuery library referenced you can now pull the current year and push it to an HTML element using some jQuery syntax. The below JavaScript grabs the current year and pushes the value to the “text” of the HTML element with the ID of “year”.
<script type="text/javascript">
var currentYear = (new Date).getFullYear();
$(document).ready(function() {
$("#year").text(currentYear);
});
</script>
Now we create the HTML element to receive the year value:
<p>Copyright <span id="year">TEXT</span> Company Name - All Rights Reserved.</p>
With this code in place, the year will be pushed to the TEXT position of the “year” span tag when the DOM is ready. This functionality is useful for elements such as copyright dates or other date representations that need to stay current.