ASP.NET: Setup the Page Title from Web.Sitemap

Wednesday, August 04, 2010

ASP.Net allows you to associate a sitemap file with your project. You can add this file as a Web.Sitemap in the web project. This file could be used with the ASP.Net navigation controls to create menu, tree view or a site map path on your website. One of the other things where this file comes handy is when you want to dynamically assign title to your pages. This is a fairly common requirement when you use master pages and don't want to modify additional parameters on individual pages. Here's the code you should add into your master page's .cs file:

// C#
protected override void OnInit(System.EventArgs e)
{
base.OnInit(e);

// Setting Page Title from SiteMap
SiteMapNode node = SiteMap.CurrentNode;

if (node != null && !string.IsNullOrEmpty(node.Title))
{
Page.Title = "App Name | " + node.Title;
}
}
' VB.NET
Protected Overrides Sub OnInit(e As System.EventArgs)
MyBase.OnInit(e)

' Setting Page Title from SiteMap
Dim node As SiteMapNode = SiteMap.CurrentNode

If node IsNot Nothing AndAlso Not String.IsNullOrEmpty(node.Title) Then
Page.Title = "App Name | " + node.Title
End If
End Sub

The above function adds the associated title associated with the url of the page.

this.blog was Hibernating, but @gsmodi was Awake

Thursday, March 18, 2010

It has been a long time since I last posted on this blog - the longest interval between any two consecutive posts on this blog. Posting long posts on the blog has suffered because of the convenience and ease of posting something in less than 140 characters on Twitter (@gsmodi). The USP of Twitter is its simplicity. The fact that you can use it from multiple devices and applications still getting all the required functionality makes it stand out.

This blog now goes into hibernation rather more frequently than anytime in the past. Its just not about me losing interest in blogging or having a busier schedule; its about the plethora of other social networks which take up the time I might have spend on blogging. Facebook and Twitter are the hot suites of this era. I am regular on both the network though seldom like to post on Facebook, but do tweet rather frequently. Still trying to decipher why I don't like to update my status on Facebook but can tweet about just anything (related tweet). I am sure there are many who share the same syndrome. Another network which I frequent is LinkedIn, which I believe is an excellent way for professional networking and getting updates about your contacts.

The arena of social networks is perpetually being enhanced. If we try to look through the past few years, social networks have evolved marvelously. Everyone wants a share of this huge pie. While Facebook might be leading the race at this time (some people also talk about it as the next big thing after Google), these sites have a history of being overturned by competition. We all have seen the fate of MySpace, Orkut and the likes. Though each have been successful in their times in particular regions of the world, they tend to die out and are superseded by the competition. With Google coming up with Buzz, fresh networks like Tumblr gaining popularity and a plethora of other services which are launched everyday, this would be an interesting space to watch.

OK, that was a bit about social networks - was not supposed to be in this post, could have written a complete post on that stuff. So, coming back to the main point, should I let this blog die out? I don't think so, I've been maintaining it for over 4 years now. I'll try to be more regular, maybe write shorter posts more frequently and cut down the time to do some research before posting about something. Lets see how that turns out.

@gsmodi (Twitter) will hopefully still be awake and this.blog would try hard to wake up ;)

Program to Compute Large Fibonacci Numbers in Hex

Sunday, December 27, 2009

Here's a Java program that calculates the n'th Fibonacci number in hexadecimal format, where n could be infinitely long. For those who don't know, Fibonacci series is a series which is calculated by summing up the last two numbers of the series. It starts from 1, 1, 2, 3, 5, 8, 13, 21...

Large Fibonacci numbers are particularly useful to calculate the Golden Ratio, which can be calculated by using the ratio of the last 2 infinitely large terms of the Fibonacci series. The approximation of the ratio improves as you progress farther in the series. For more interesting mathematical properties read the Wikipedia entry for Golden Ratio.

In this program, the hex values are stored in string because of the restrictions posed by the conventional data types. The series is calculated by summing up the last 2 numbers of the series using carry and adding digit by digit. Eg. 8 & 13 can be added by adding 8+3 and have one in unit's place and carry 1 to the ten's place, then adding 1+1 = 2; thus, the answer 21.

public class FibonacciNumber {

public static void main(String[] args) {

String h1 = "1"; // First Fibonacci Number
String h2 = "1"; // Second Fibonacci Number
String h3 = ""; // Next Fibonacci Number
String tempResult = "";
int i1=0, i2=0, i3;
Long ctr = new Long(0);

try {
// Loop till maximum value of Long
// Each run calculates the next number
while (ctr < Long.MAX_VALUE) {

i3 = 0;
h3 = "";
ctr++;

// This loop calculates the sum of the previous numbers digit by digit
for (int i = h1.length(), j = h2.length(); j > 0; i--, j--) {

if (i > 0) { // If a digit is left in i
i1 = Integer.parseInt(h1.substring(i - 1, i), 16);
i2 = Integer.parseInt(h2.substring(j - 1, j), 16);
i3 += i1 + i2;
} else if (h2.length() > h1.length()) {
// When h1 is shorter than h2. Eg. h1=8 h2=11
i2 = Integer.parseInt(h2.substring(j - 1, j), 16);
i3 += i2;
}

tempResult = Integer.toHexString(i3);

if (tempResult.length() == 2) // Handling carry digit
{
i3 = Integer.parseInt(tempResult.substring(0,1), 16);
h3 = tempResult.substring(1,2) + h3;
}
else // No carry digit
{
i3 = 0;
h3 = tempResult.substring(0,1) + h3;
}
}
if (i3 != 0)
h3 = tempResult.substring(0,1) + h3;

System.out.println(ctr + ": " + h3);

h1 = h2;
h2 = h3;

}
} catch (Exception ex) {

System.out.println(ex.toString());
}

}
}


The output of the program will look like:

1: 2
2: 3
3: 5
.
.
244: 3144535de4cae86ae251b1971896d58bf961e6b9d48
245: 4fb72becbf73267006f65a3aa504c57b1e9930e392d
246: 80fb7f4aa43e0edae9480bd1bd9b9b0717fb179d675
.
.
846: bc35110da32840cce42425afddfe8fde936030e7fda1d532615324be2a410804acef43394c0b512d5e34a2f5e77fbe0e82d80580f4fb592de0232add6f7e6f34127e8eb43165d008fe5
847: 130869a782cc70170bd47e47fb3034ecbbce810b2e7dce2a56c30baa1f3bc359eb13f26ed83e0f4357dbed6c374b79dfcf303e52c94a3996b1dfff19229baa1c0194727a75d25313cd62
.
.
Can go upto zillions of numbers.