Datasheet

1 2 3 4 5 6 7 8 9 10
Hacks Revisited
Hacks exist in an ever-changing world. One day they are the cutting-edge, clever inventions of a
developer with a need. Another day in the future, they can become mainstream code or practices
that are integrated into a product or process. The mark of a successful hack is that one day it will
become the norm.
This chapter is all about hacks that have become successful. We revisit ASP.NET v1.1 to look at a
group of hacks that received much attention. They became so useful that Microsoft felt compelled
to add support for them in ASP.NET v2.0. Rather than redo hacks that have already been invented,
this chapter highlights the pioneers and their work that influenced Microsoft to add support in
ASP.NET v2.0. For those of you making the move to ASP.NET 2.0, you’ll learn how these hacks
have been integrated into the .NET Framework and related Visual Studio 2005 support. Seeing
how previous hacks have positively influenced the current version of ASP.NET can give you and
others a greater appreciation for hacks, and provide motivation for new hacks that will have a pos-
itive influence on future versions of ASP.NET.
Wizards Hacks Replaced by ASP.NET 2.0
Wizards are user interface tools for gently guiding users through a process. This discussion refers to
wizards that you add to your applications. Developers have used them for many years, from instal-
lation programs to simplifying complex activities through a series of steps. They’ve traditionally
been a normal part of client desktop applications, but have surfaced as hacks in Web applications.
ASP.NET Wizard Pioneers
An early ASP.NET v1.1, a wizard hack by John Peterson was published as a sample on ASP101.com
(
asp101.com/samples/wizard_aspx.asp)titled “ASP.NET version of Wizard (Multi-Page Form).”
This was an update from a previous implementation for classic ASP 3.0. After support for ASP.NET
2.0 wizards was announced, Tom Blanchard wrote the article “CodeSnip: Simulating the ASP.NET 2.0
Wizard Control with ASP.NET 1.x.” at
123aspx.com/redir.aspx?res=32798. Another pre-existing
04_597663 ch01.qxp 4/25/06 9:54 PM Page 1
COPYRIGHTED MATERIAL

Summary of content (24 pages)

As you can see from the steps of Listing 1-1 and Figure 1-8, the user is either a Meat Eater or a Vegetarian.

  • PAGE 9

    Hacks Revisited Listing 1-2: Altering the sequential progress of a wizard protected void Wizard1_NextButtonClick( object sender, WizardNavigationEventArgs e) { if (Wizard1.ActiveStepIndex == 0) { if (RadioButtonList1.SelectedValue == “Vegetarian”) { Wizard1.ActiveStepIndex = 2; } } if (Wizard1.ActiveStepIndex == 1) { Wizard1.ActiveStepIndex = 3; } } By capturing the NextButtonClick event, as shown in Listing 1-2, you can control the navigation sequence of the Wizard.

  • PAGE 10

    Chapter 1 In Listing 1-3, we’re primarily interested in whether the eating preference was Meat Eater or Vegetarian so we can extract values from the proper controls. The code emits, via Response.Write, the selected menu items, depending on whether the user selected Meat Eater or Vegetarian when running the Wizard. Going through the process of adding steps, handling events, and altering properties, you can see how sophisticated the ASP.NET v2.0 Wizard control is.

  • PAGE 11

    Hacks Revisited To get started, create a new Web project and delete the Default.aspx page that is automatically created. We’ll re-create Default.aspx later as a content page that uses a Master Page we’re about to create. 1. To create a Master Page, right-click on the Web project in Solution Explorer, select Add New Item, select Master Page, give it the name Company.master, and click the OK button. This creates a new Master Page. 2.

  • PAGE 12

    Chapter 1 Figure 1-10 After creating and modifying the Company.master Master Page, you can view the automatically generated HTML code by clicking the HTML button at the bottom-left corner of the editor. Listing 1-4 shows what the HTML for the Company.master Master Page looks like after making the modifications described in the previous paragraph. Listing 1-4: A Master Page with formatting for custom page elements: Company.master <%@ Master Language=”C#” AutoEventWireup=”true” CodeFile=”Company.master.

  • PAGE 13

    Hacks Revisited

    My Company Header

    Menu Goes Here
    Copyright (C) 2006 My Company, All Rights Reserved

  • PAGE 14

    Chapter 1 Listing 1-5 (continued) ContentPlaceHolderID=”ContentPlaceHolder1” Runat=”Server”>

    This is my content.

    The two interesting parts of Listing 1-5 are the MasterPageFile attribute and the Content element. The Page directive contains a new attribute named MasterPageFile, specifying the filename of the Master Page to use. It is evident from Listing 1-5 that content pages don’t have HTML markup as normal Web Forms do.

  • PAGE 15

    Hacks Revisited No matter how query string parameters are presented, average users will have a hard time understanding. Instead, it would be much clearer to write the query string with a nice hierarchical representation, like this: http://www.someblogsite.com/username/2005/January/31 For a typical visitor of a site, the preceding URL is not too hard to figure out. For example, if users were to remove the day, they would see all of the entries for the entire month of January.

  • PAGE 16

    Chapter 1 Listing 1-6 (continued) .NET Article Archive

    .

  • PAGE 17

    Hacks Revisited May
    June
    July
    August
    September

  • PAGE 18

    Chapter 1 Listing 1-8 (continued) SetMonths(); } ///

    /// configure months to refer to proper page /// private void SetMonths() { foreach (Control ctrl in pnlMonths.Controls) { HyperLink monthLink = ctrl as HyperLink; if (monthLink != null) { monthLink.NavigateUrl = monthLink.NavigateUrl.Replace(“YEAR”, year); } } } /// /// set page title /// private void SetTitle() { lblTitle.

  • PAGE 19

    Hacks Revisited

  • PAGE 20

    Chapter 1 Listing 1-10 (continued) Being an advocate of well-designed n-tier architectures, I dec

  • PAGE 21

    Hacks Revisited using System.Data; using System.Web; ///

    /// Summary description for ArticleData /// public class ArticleData { public void GetArticles(List
    articles, string year, string month) { DataSet dsArticles = new DataSet(); dsArticles.ReadXml(HttpContext.Current.Server.MapPath(“Articles.xml”)); DataView dvArticles = new DataView(dsArticles.Tables[“article”]); dvArticles.

  • PAGE 22

    Chapter 1 Listing 1-13 (continued) 2005 02 Title2 This is the text

    2005 02 Title3 This is the text
    2006 01 Title4 This is the text
    2006 01 Title5 This is the text
    2006 02 Title6

  • PAGE 23

    Hacks Revisited private string m_year; public string Year { get { return m_year; } set { m_year = value; } } private string m_month; public string Month { get { return m_month; } set { m_month = value; } } private string m_title; public string Title { get { return m_title; } set { m_title = value; } } private string m_content; public string Content { get { return m_content; } set { m_content = value; } } public Article(string year, string month, string title, string content) { Year = year; Month = month; T

  • PAGE 24

    Chapter 1 Using the ASP.NET v2.0 URL Mapping feature appears to be a great capability, but there is one catch: You can’t use regular expressions. The articles example in this section could have been better written using regular expressions. Notice that the urlMappings elements contain very similar code that only differs by year or month. This could have been implemented better with a single regular expression that mapped the year and month with parameter placement.