To start, create a new web site in VS2005 and name it DebuggingApp. This will consist of a single web page containing a header label, a DropDownList with a label below it to display the selected item, and a hyperlink.
Drag a Label control to the top of the page and set its Text property to:
Tracing, Debugging & Error Handling Demo
Change its Font.Name property to Arial Black, its Font.Size property to Large, and its Font.Bold property to true .
Place a DropDownList control on the form. Name it ddlBooks . Change its AutoPostBack property to true . The drop-down list's event handling code needs to be added. In Design view, click on the lightning bolt Events icon at the top of the Properties window, and double-click on the field next to SelectedIndexChanged . The code-behind file, Default.aspx.cs , will open , and the cursor will be located in the event handler method ddlBooks_SelectedIndexChanged . Type in the highlighted code from Example 7-1.
protected void ddlBooks_SelectedIndexChanged(object sender, System.EventArgs e) { // Check to verify that something has been selected. if (ddlBooks.SelectedIndex != -1) { lblDdl.Text=ddlBooks.SelectedItem.Text + " ---> ISBN: " + ddlBooks.SelectedItem.Value; } }
Add a label below the DropDownList called lblDdl . Set the Text property so it is empty.
In the Page_Load method in the code-behind file, add the code from Example 7-2.
private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if (! IsPostBack) { // Build 2 dimensional array for the lists // First dimension contains bookname // 2nd dimension contains ISBN number string[,] books = { {"Programming C#","0596006993"}, {"Programming .NET Windows Applications","0596003218"}, {"Programming ASP.NET","0596004877"}, {"WebClasses From Scratch","0789721260"}, {"Teach Yourself C++ in 21 Days","067232072X"}, {"Teach Yourself C++ in 10 Minutes","067231603X"}, {"XML & Java From Scratch","0789724766"}, {"Complete Idiot's Guide to a Career in Computer Programming","0789719959"}, {"XML Web Documents From Scratch","0789723166"}, {"Clouds To Code","1861000952"}, {"C++ Unleashed","0672312395"} }; // Now populate the lists. int i; for (i = 0; i < books.GetLength(0); i++) { // Add both Text and Value ddlBooks.Items.Add(new ListItem(books[i,0],books[i,1])); } } }
Finally, add a HyperLink control below lblDdl . Name it hplTest . Change the Text property to Link To and change the NavigateUrl property to TestLink.aspx . No page with this name exists. This is an intentional error to demonstrate error handling later in the chapter.
Run the web page and select one of the items in the drop-down list; you should see something like Figure 7-1.
You will use this application through the rest of this chapter to demonstrate various techniques for analyzing and debugging code in ASP.NET and for handling errors in your application.