精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
锐英源精品开源心得,转载请注明:“锐英源www.wisestudy.cn,孙老师作品,电话13803810136。”需要全文内容也请联系孙老师。中文译文
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:
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:
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:
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:
<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.主页面是ToDoList.aspx.