7.1 Creating the Sample Application

To start, create a new C# or VB.NET web application project in Visual Studio .NET and name it DebuggingApp. This project 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.

Change the pageLayout property of the document from the default GridLayout to FlowLayout. Put a Label control on the top of the page and set the text (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. The exact steps for doing so are different in C# than in VB.NET.

In C#, click on the Events icon at the top of the Properties box, click on the field next to SelectedIndexChanged, and type in the event handler name ddlBooks_SelectedIndexChanged.

In VB.NET, display the code-behind source code window by clicking on the code-behind file tab at the top of the source code window. At the top of the window are two drop-down lists. The list on the left contains all the objects on the page. Select ddlBooks from that list. The drop-down on the right will contain all the available events. Select SelectedIndexChanged. Alternatively, double-click on the control in the Design view of the .aspx file. In either case, this will immediately open the code-behind page with the cursor inside the event handler method with that name, ready for you to add the event handling code.

Type or paste in the code in Example 7-1, if you're programming in C#, or Example 7-2, if you're programming in VB.NET. These examples are excerpted from Example 5-25 and Example 5-26, respectively.

Example 7-1. SelectedIndexChanged event handler in C#
private 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;          } }
Example 7-2. SelectedIndexChanged event handler in VB.NET
Sub ddlBooks_SelectedIndexChanged(ByVal Sender as Object, _                              ByVal e as EventArgs)    '  Check to verify that something has been selected.    if ddlBooks.SelectedIndex <> -1 then       lblDdl.Text=ddlBooks.SelectedItem.Text & " ---> ISBN: " & _          ddlBooks.SelectedItem.Value    end if End Sub

Add the code shown in Example 7-3, if you're using C#, or Example 7-4, if you're programming in VB, to replace the Page_Load event. (Again, these examples are excerpted from Example 5-25 and Example 5-26, respectively.)

Example 7-3. Page_Load event handler in C#
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#","0596001177"},             {"Programming ASP.NET","1234567890"},             {"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++: An Introduction to Programming","1575760614"},             {"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]));       }    } }
Example 7-4. Page_Load event handler in VB.NET
Private Sub Page_Load(ByVal sender As System.Object, _                       ByVal e As System.EventArgs) Handles MyBase.Load    'Put user code to initialize the page here    if not IsPostBack then       '  Build 2 dimensional array for the lists       '  First dimension contains bookname       '  2nd dimension contains ISBN number       dim books(,) as string = { _          {"Programming C#","0596001177"}, _          {"Programming ASP.NET","1234567890"}, _          {"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++: An Introduction to Programming","1575760614"}, _          {"C++ Unleashed","0672312395"} _       }       '  Now populate the lists.       dim i as integer       for i = 0 to books.GetLength(0) - 1          '  Add both Text and Value          ddlBooks.Items.Add(new ListItem(books(i,0),books(i,1)))       next    end if  End Sub

Add a label below the DropDownList called lblDdl. Set the Text property so that it is empty.

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. Note that no page with this name actually 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.

Figure 7-1. Sample page for tracing, debugging, and error handling
figs/pan2_0701.gif


Programming ASP. NET
Programming ASP.NET 3.5
ISBN: 0596529562
EAN: 2147483647
Year: 2003
Pages: 156

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net