Completing the Sample Application


It's now time to add the remaining code to the sample application to make it fully functional. There are a few things missing. First of all, you might have noticed that the sample code thus far creates an instance of the ToDoList when the application starts, and creates instances of the ToDoItem class when the user clicks the Add Task button, but there is nothing there yet to connect the two instances. We need to add code that adds the ToDoItem to the list. To accomplish this you'll have to learn about the Application object.

The Application object is provided by ASP.NET. It enables you to maintain information for all the users that access your application. The Application object is like a large filing cabinet. It lets you add any object you want to the filing cabinet and assign a name to the item. The item is known as the key. In fact it works similarly to the HashTable class that you learned about previously. The key enables you to access the item later on, and even to remove it. The Application object makes it handy to pass information from one Web page to another.

All users using the Web application have the same Application object. Therefore, if one user adds tasks to the list, a different user will be able to see those items. When any user clicks View Tasks, that user sees all the tasks from all the users.

Another object that lets you save information is called the Session object. Session object works per user. Each user gets its own Session object; when you add information to the Session object, that information is only available to the current user.. We will use the Session object throughout this book.

It would also be nice to be able to display the list of tasks when the user clicks the View Tasks link. For that we are going to add another page with a ListBox control in it.

So, let's get to it.

To connect the ToDoList to ToDoItems:

  1. Right-click on Global.asax and choose View Code from the context menu.

  2. In the function Application_Start add the highlighted code from Figure 2.80 to complete it.

    Figure 2.80 Add this code to the Global.asax file. The code creates a ToDoList object and adds it to the Application object.
     protected void Application_Start(           Object sender, EventArgs e) {    ToDoList todo;    todo = new ToDoList();  Application.Add("list",todo);  } 
  3. Right-click on entertask.aspx and choose View Code from the context menu.

  4. Enter the highlighted code from Figure 2.81 . Notice that some of the code is meant to be added as part of the btnTask_Click function. Then add the ClearFields function right before it.

    Figure 2.81 This code retrieves the ToDoList object from the Application object and then adds a ToDoItem to it.
     //ClearFields simply does //what the name implies //it resets the textboxes in //the form. We call this function //after adding a task to the list. void ClearFields()  {   txtItem.Text = "";   txtDescription.Text = "";   }  private void btnTask_Click(object sender,              System.EventArgs e) {    ToDoItem item = new ToDoItem();    item.SetInfo(txtItem.Text,    txtDescription.Text);    //Retrieve the ToDoList from the    //global Application object, and add    //the newly created ToDoItem to the    //list. Then clear the form fields.    //Because many clients may be    //accessing the same list    //simultaneously make sure the code is    //thread-safe with Lock and Unlock.  Application.Lock();   ToDoList list =   (ToDoList)Application["list"];   list.Add(item);   Application.UnLock();   ClearFields();  } 

    The only thing left is to find a way to display the items in the task list. For this we are going to use a technique called databinding. Databinding makes it easy to display the items in the list. All we have to do is connect a "data-aware" control to the object that holds the list and the control reads and displays all the items in the list. Let's see how that's done.

To display the items in the task list:

  1. Select Project > Add Web Form from the main menu. You should see the dialog in Figure 2.82 .

    Figure 2.82. The Add New Item dialog is used to create a new Web Form file and add it to your project.

    graphics/02fig82.gif

  2. Change the default file name to viewlist.aspx and press Enter. The wizard adds viewlist.aspx to the Solution Explorer.

  3. Double-click viewlist.aspx to reveal the form.

  4. Add a ListBox control and a label control to the form. Customize the form so that it looks like the form in Figure 2.84 ( next page). Of course this is really hard to do without detailed instructions. You may want to enter the HTML in Figure 2.83 directly, the way you learned at the beginning of the chapter, or even better, copy and paste the HTML from the downloadable sample code at www.peachpit.com/vqs/csharp.

    Figure 2.83 You can use this HTML to create a simple form that displays the list of to-do items.
     <%@ Page language="c#"            Codebehind="viewlist.aspx.cs"            AutoEventWireup="false"            Inherits="classesandmembers.viewlist" %> <HTML>     <HEAD>      <title>viewlist</title>    </HEAD>    <body MS_POSITIONING="GridLayout">      <form id="viewlist" method="post"      runat="server">          <asp:  ListBox  id="lstTaskList"               style="Z-INDEX: 101;               LEFT: 35px;               POSITION: absolute;               TOP: 46px"               runat="server" Width="183px"               Height="200px">            </asp:ListBox>            <asp:  Label  id="Label1"               style="Z-INDEX: 102;               LEFT: 39px;               POSITION: absolute;               TOP: 17px"               runat="server"               Width="175px">  Items:  </asp:Label>      </form>   </body> </HTML> 
    Figure 2.84. The viewlist.aspx form has a listbox that will display the to-do items. This listbox will be data bound. Databinding is a quick way to populate a control from a collection, like a HashTable.

    graphics/02fig84.gif

  5. After adding the controls and naming them correctly according to Figure 2.84 , doubleclick in an empty space on the form. Doing so should bring up the code editor positioned in the Page_Load function.

  6. Add the code in Figure 2.85 to the Page_Load function.

    Figure 2.85 The listbox control has a DataSource property that you can set to a collection. Collections will be discussed in Chapter 9, "Arrays and Collections." By calling the DataBind method the control reads all the items in the collection and adds them to the list.
     private void Page_Load(object sender, System.EventArgs e) {     //grab the ToDoList object from the     //Application and bind it to the     //TaskList ListBox (lstTaskList)  ToDoList list =   (ToDoList)Application["list"];   lstTaskList.DataSource =   list.TaskList;   lstTaskList.DataBind();  } 

    The last thing to do is to make the internal list in the HashTable (the object the ToDoList object uses to maintain the list of ToDoItem's ) available to the viewlist function. This is easily done by adding a read-only property to the ToDoList class.

  7. Right-click on entertask.aspx and choose View Code from the context menu.

  8. Add the highlighted code from Figure 2.86 to the ToDoList class.

    Figure 2.86 The HashTable stores objects. Each object has a key assigned to it. You can get the list of items using the Values property of the HashTable.
     class ToDoList {    Hashtable list = new Hashtable();    public void Add(ToDoItem tditem) {      list.Add(tditem.Item,tditem.Description); }      public void Remove(ToDoItem tditem)      {         list.Remove(tditem.Item);      }  public ICollection TaskList   {   get   {   return list.Values;   }   }  } 
  9. Press F5 to build the code and execute.

graphics/tick.gif Tip

  • There are a couple of things in the sample code that have not been explained fully. Don't worry about knowing all the details at this point. Things like HashTables will be explored in more detail throughout the book. Other concepts like databinding are beyond the scope of this book. This book concentrates on the C# language itself and not on Web-specific concepts. Nevertheless, you should get a good understanding of those concepts just by trying the code in the examples. Consult the MSDN documentation that ships with Visual Studio for more information on databinding.




C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

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