What Do You Need to Run ASP.NET?
ASP.NET is supported on the following operating systems:
□ Microsoft Windows 2000 Professional and Server (SP2 recommended)
□ Microsoft Windows XP Professional
□ Microsoft Windows Server 2003/2008
□ Microsoft Windows Vista
To run ASP.NET applications, you need to install IIS on your computer (IIS is not installed by default; you can install IIS on your computer by running the Add or Remove Programs application in the Control Panel and then selecting the Add/Remove Windows Components tab). To obtain the ASP.NET runtime, you must install the .NET Framework on your machine. You can obtain the latest .NET Framework from the following site: http://microsoft.com/downloads.
One of the most common tasks a web application does is display records from a database. For example, you may have an inventory web application with which your staff can check the latest pricing information and stock availability. This chapter explains how to retrieve records from a database and use data binding in ASP.NET to display them on a page. In addition, it shows how to use the new LinqDataSource
control, which enables you to use LINQ to talk to databases without needing to write complex SQL queries.
To start, launch Visual Studio 2008 and create a new ASP.NET Web Site project (see Figure 17-1).
Figure 17-1
The default location is File System (see Figure 17-2), which means that you can save your ASP.NET project in any folder on your local drive so that during debugging a built-in web server is automatically launched to host your ASP.NET application. Alternatively, you can choose the HTTP option, which means that your ASP.NET application will be hosted by a web server (most commonly the local IIS), or the FTP option, which uses an FTP Server. For this example, use File System, the default option.
Figure 17-2
Modeling Databases Using LINQ to SQL
The example web application will display records from two tables in the pubs sample database. Because you are going to use LINQ to access the database, you do not connect to the database directly. Instead, you generate classes that represent the database and its tables and then use those classes to interact with the data. To begin, add a new item to the project and select the LINQ to SQL Classes template (see Figure 17-3).
Figure 17-3
Use the default name of DataClasses.dbml
. When prompted to save the item in the App_Code
folder, click Yes. The DataClasses.dbml
file is created in the App_Code folder of your project (see Figure 17-4).
Figure 17-4
The Object Relational Designer (O/R Designer) then launches so that you can visually edit the databases and tables you want to use. Open the Server Explorer window, and connect to the pubs
sample database. Drag and drop the publisher
and title
tables onto the design surface of DataClasses.dbml
(see Figure 17-5).
Figure 17-5
Save the DataClasses.dbml
file by pressing Ctrl+S. When you save the file, Visual Studio 2008 persists out .NET classes that represent the entities and database relationships that you have just added. For each LINQ to SQL designer file you add to your solution, a custom DataContext
class is generated. It is the main object that you use to manipulate the table. In this example, the DataContext
class is named DataClassesDataContext
.
Be sure to save DataClasses.dbml
before proceeding.
Data Binding Using the GridView Control
To display the records from a table, you can use the GridView
control, which displays the values of a data source in a table where each column represents a field and each row represents a record. Drag the GridView
control from the Toolbox and drop it onto the design surface of Default.aspx
. In the SmartTag of the GridView
control, select in the Choose Data Source dropdown list (see Figure 17-6).
Figure 17-6
In the Data Source Configuration Wizard (see Figure 17-7), select LINQ and click OK. Use the default name of LinqDataSource1
. Click OK.
Figure 17-7
For those of you familiar with the various data source controls (such as SqlDataSource
and ObjectDataSource
) in ASP.NET 2.0, the LinqDataSource
control works much like them. What is special about the LinqDataSourcecontrol is that instead of binding directly to a database (as with the SqlDataSource
), it binds to a LINQ-enabled data model. The beauty of this is that you need not write the various complex SQL queries (such as insert
, delete
, and modify
) to use it. Instead, you just need to specify the data model you are working with, and the type of operations you want to perform on it (such as delete
, insert
, or update
) and then the control takes care of performing those operations by itself.
The DataClassesDataContext
object that you generated earlier is automatically selected for you (see Figure 17-8). Click Next.
Figure 17-8
Select the titles
table, and click the * checkbox to select all fields (see Figure 17-9).
Figure 17-9
Click the Advanced button and check all the checkboxes. Click OK (see Figure 17-10) and then click Finish.
Figure 17-10
Switch to the source view of Default.aspx
page, and observe the element:
ID="LinqDataSource1" runat="server"
ContextTypeName="DataClassesDataContext"
EnableDelete="True"
Читать дальше