Click here to Skip to main content
15,868,016 members
Articles / Web Development / ASP.NET
Article

ASP.NET To Do List Application

Rate me:
Please Sign up or sign in to vote.
4.81/5 (46 votes)
13 Jun 20027 min read 583.9K   20.5K   212   93
A simple application that demonstrates key ASP.NET features.

Overview

To Do List Application in Internet Explorer 6.0

I recently attended an excellent two day ASP.NET workshop given by the folks at Wintellect. After I returned to my desk, I decided I need to write an application using ASP.NET in order to make the things I had learned stick in my brain. This simple application is the result. However, this simple two page web application demonstrates how to use all of the following ASP.NET features:

  • Web controls, including the awe inspiring DataGrid control (I'm not kidding).
  • DataGrid template columns.
  • Web form data binding.
  • Dynamic control creation.
  • View state.
  • Code render blocks.
  • Data binding expressions.
  • Code behind.

In addition to the above, there's also code showing how to do simple ADO.NET actions, using CSS within Web Forms, and I even threw in a little JScript for variety! I used C# as the code-behind programming language, again for the learning experience.

Installation & Setup

I'll start with the installation and setup of the sample code. Microsoft has boasted that ASP.NET applications can be "XCOPY" installed. (If you don't know what XCOPY is, go to a command prompt and type HELP XCOPY.) This is almost true. You can certainly copy an .aspx file to your \inetpub\wwwroot directory, then open it with your browser and have the page execute and display. This is in fact how I started building the application. Very quickly, I found out that I would need to use a debugger to figure out what was going on, so I moved the project inside Visual Studio .NET to get more control, and I never looked back. In a nutshell, here's what you will have to do to install the project files correctly, so that you can build and debug the code with VS.NET:

  1. Unzip the project source into a todo or other directory under your \inetpub\wwwroot directory.
  2. Open up the IIS MMC snap-in, or the Internet Services Manager. Browse to the directory and select Properties. Under the Application Settings section on the Directory tab, click on Create. This makes IIS and VS.NET happy.
  3. Edit the file ToDo.csproj.webinfo and set the URLPath to the correct URL to get to whatever location you stashed the project in for 1.
  4. In the web.config file, set the location of the Microsoft Access database correctly down in the <appSettings> tags (see below).
  5. You must give the local machine ASPNET account Modify, Read & Execute, Read and Write permissions to the file todo.mdb, or you will get the somewhat confusing error message "Operation must use an updateable query" when trying to update entries. Welcome to the world of secure programming!

When you've done this, you should be able to double click on Todo.csproj and open the project in VS.NET. Note that I manually created a Default.htm file that redirects to the ToDoList.aspx file, as I prefer this to adding the default pages to the IIS configuration. This allows you to hit, for example, http://localhost/todo and bring up the application.

Source Code Overview

I'm not going to attempt a soup to nuts ASP.NET tutorial here. I will assume that if you're interested in this sample, you are willing to do your own background reading of the help files. Here, I will just highlight the key points of the code.

First, a word about the database. I've used Access 2000 and ADO.NET to do the data access for the application. This is by far the easiest way to get up and running. I used the OleDbDataAdapter class in conjunction with raw SQL almost exclusively. The following code snippet is common in using ADO.NET and ASP.NET:

C#
string connStr = ConfigurationSettings.AppSettings["ConnectionString"];
string queryStr = "select * from Items where id=" + idStr;
OleDbDataAdapter adapter = new OleDbDataAdapter(queryStr, connStr);

DataSet ds = new DataSet();
adapter.Fill(ds);

Note also that I stored the connection string for the database in the application configuration file, as described in the installation steps. In the web.config file, look for the following section:

XML
<appSettings>
    <add key="ConnectionString" 
       value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=
                  C:\Inetpub\wwwroot\todo\ToDo.mdb" />
</appSettings>

The main page is ToDoList.aspx. A snapshot is shown at the top of this document. My goals for this page where to show the list of to do items, allow some simple filtering, and to allow add, modify, delete and closure of items. In the code, you can see that it's quite OK to mix ASP.NET web controls with good old fashioned HTML and CSS. In fact, it's Microsoft recommendation that for performance reasons, you don't use a Web Control until you need to generate some content dynamically.

XML
<asp:TemplateColumn ItemStyle-Width="12">
    <ItemTemplate>
    <img 
          src='<%# _priorityUrls[(int)DataBinder.Eval(Container.DataItem,
                             "Priority") - 1] %>'/>
    </ItemTemplate>
</asp:TemplateColumn>

This code shows how to add a template column to a DataGrid. In this case, I'm using to display an icon for the to do item priority. A key thing to note here is the use of the single quotes (') around the src= tag. It's not documented, but this appears to be the only way to get this data binding expression to work as the contents of an attribute tag.

In the code behind C# for this page, note how we dynamically create columns, depending on the query selected. For example:

C#
bcOpened = new BoundColumn();
bcOpened.HeaderText = "Opened";
bcOpened.DataField = "Opened";
ToDoDataGrid.Columns.Add(bcOpened);

This code dynamically adds a bound column to the ToDoDataGrid, in this case, to show the date the to-do item was first opened.

Another interesting code to examine in this file is the way that the Edit, Delete, Close and Reopen commands are handled:

C#
public void ToDoDataGrid_Command(Object sender, DataGridCommandEventArgs e)

The way the databound grid columns work is that there's a little snippet of JScript code that runs when you click on any of the Edit/Delete/Done/Reopen anchors. This JScript causes an HTTP POST request to be sent back to the ASP.NET application.

One piece of code may have you scratching your head for a while, and that is the following:

C#
int query = 2;

if (IsPostBack) 
{ 
    query = (int)ViewState["query"]; 
} 
else 
{
    string queryStr = Request.Params["query"]; 
    
    if (queryStr != null) 
        query = Int32.Parse(queryStr);
    
    ViewState["query"] = query;
}

What I'm doing on this page is using a parameter on the URL to indicate which one of three grid layouts to display. If the parameter is missing, we just default to showing the open to-do items. However, when the HTTP POST requests come back to us when the user clicks on any of the bound column anchors, the query parameter is missing from the request, so we have no idea what grid we were looking at. Never fear, ASP.NET view state is here to save the day. The view state is actually implemented as a hidden field in the generated HTML that gets sent back with all the other HTTP POST data. It basically contains a bunch of serializes .NET objects for all of the web controls on your form, and the great part is you can add your own stuff to it. In this example, we are actually using the view state for the web form itself, but we could have attached the value to the view state for the grid in much the same way.

The code in ToDoEdit.aspx contains a little bit of JScript to set focus to the control on the form. You'd think this would be part of the ASP.NET framework, but it does not appear to be.

HTML
<script for="window" event="onload">
window.document.forms["EditItemForm"].children["DescriptionTextBox"].focus();
</script>

Notice also that the OnSubmit method can get called either by pressing the Enter key, or by clicking on the anchor. When submit occurs via the Enter key, it actually comes in via a post back on the page. When the anchor is clicked, it comes back via the SaveButton_Click method.

Conclusion

I have to say that ASP.NET makes building great looking, powerful and useful web applications really easy. The real hurdle is learning about the .NET Framework classes. There are literally thousands of classes to help you write applications faster. The problem is that when you start, you either won't know that a class exists, or how to use it correctly. Hopefully, this sample will help you get started with your ASP.NET development that much more quickly, by demonstrating how to use some of the key ASP.NET classes. I think you'll end up agreeing that ASP.NET is the killer feature of Microsoft's .NET initiative.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
When I was 14 my physics teacher dragged a dusty Commodore PET out of a cupboard and asked, "Do you think you could do anything with this?"

Comments and Discussions

 
Questionthanks Pin
mohamad yousef21-Dec-11 0:52
mohamad yousef21-Dec-11 0:52 
AnswerRe: thanks Pin
dudis14-May-12 2:22
dudis14-May-12 2:22 
AnswerRe: thanks Pin
dudis14-May-12 2:23
dudis14-May-12 2:23 
QuestionWhat I found on VS 2008 and Windows Server 2003 Pin
DavidPollard23-May-08 18:15
DavidPollard23-May-08 18:15 
GeneralI like it but what if.. Pin
gandhisunny_god19-Apr-07 22:41
gandhisunny_god19-Apr-07 22:41 
GeneralThx Pin
Ilove.net29-Dec-06 5:52
Ilove.net29-Dec-06 5:52 
GeneralI have to learn more asp.net Pin
dhulipudi29-May-06 0:06
dhulipudi29-May-06 0:06 
GeneralRe: I have to learn more asp.net Pin
nature027622-Sep-06 18:40
nature027622-Sep-06 18:40 
Generalread picture Pin
sami_8221-Dec-05 15:59
sami_8221-Dec-05 15:59 
Generalread picture Pin
sami_8221-Dec-05 15:57
sami_8221-Dec-05 15:57 
Generalto do usernsme &amp;password Pin
trmpsk22-Oct-05 5:11
trmpsk22-Oct-05 5:11 
QuestionHow to get the IP client with a proxy server between it and the server Pin
maloayza17-Sep-05 4:37
maloayza17-Sep-05 4:37 
GeneralThanks !! Pin
Anonymous29-Aug-05 2:34
Anonymous29-Aug-05 2:34 
GeneralPlease help.. Pin
fire8520-Jun-05 21:03
fire8520-Jun-05 21:03 
GeneralBuild the app Pin
26-Aug-04 9:00
suss26-Aug-04 9:00 
Generalerorr message Pin
Anonymous14-Jun-04 1:51
Anonymous14-Jun-04 1:51 
GeneralParser Error Pin
Anonymous13-Jun-04 23:05
Anonymous13-Jun-04 23:05 
Generalerror message Pin
josh207827-May-04 10:13
josh207827-May-04 10:13 
GeneralAnother problem Pin
Member 72508125-Dec-03 13:24
Member 72508125-Dec-03 13:24 
GeneralJScriprt for ToDoDatagrid_Command Pin
Anonymous30-Jun-03 11:28
Anonymous30-Jun-03 11:28 
Generalututt Pin
Anonymous20-Jun-03 4:13
Anonymous20-Jun-03 4:13 
GeneralThe server block is not well formed... Pin
chetan Sharma27-May-03 8:14
chetan Sharma27-May-03 8:14 
GeneralRe: The server block is not well formed... Pin
Denny Dedhiya3-Sep-03 4:41
Denny Dedhiya3-Sep-03 4:41 
GeneralThank-You Pin
tim654389-May-03 11:54
tim654389-May-03 11:54 
QuestionDatagrid links require javascript? Pin
dazinith1-Apr-03 4:45
dazinith1-Apr-03 4:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.