Datasheet
Listing 1-8 (continued)
SetMonths();
}
/// <summary>
/// configure months to refer to proper page
/// </summary>
private void SetMonths()
{
foreach (Control ctrl in pnlMonths.Controls)
{
HyperLink monthLink = ctrl as HyperLink;
if (monthLink != null)
{
monthLink.NavigateUrl =
monthLink.NavigateUrl.Replace(“YEAR”, year);
}
}
}
/// <summary>
/// set page title
/// </summary>
private void SetTitle()
{
lblTitle.Text = “Articles for the Year “ + year;
}
}
The Page_Load method of Listing 1-8 extracts the year parameter out of the query string so it can be
used in subsequent methods. The SetTitle method uses this value to rewrite the title on the page with the
correct year.
Also, notice the
NavigateUrl properties for the HyperLink controls in Listing 1-8, where they all con-
tain the text
“YEAR” in the year’s position of the URL. This makes the code more dynamic because,
depending on the year, these
NavigateUrl properties must be rewritten. That’s what the SetMonths
method of Listing 1-8 is for. The code in Listing 1-8 deliberately places each HyperLink control into a
Panel so we can get to its
Controls collection in code. Therefore, in the code file, the SetMonths
method can simply iterate through the Controls collection and set the year with a simple
string.Replace method call. To get this built-in URL rewriting functionality to work for this applica-
tion, there is a
web.config file with a urlMapping element, as shown in Listing 1-9.
Listing 1-9: The urlMapping element in a web.config file enables URL rewriting in
ASP.NET v2.0
<?xml version=”1.0”?>
<configuration>
<system.web>
<urlMappings>
<add url=”~/2006”
mappedUrl=”~/YearView.aspx?year=2006”/>
18
Chapter 1
04_597663 ch01.qxp 4/25/06 9:54 PM Page 18