Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ASP .NET 2.0 Beta Preview - B. Evjen.pdf
Скачиваний:
26
Добавлен:
24.05.2014
Размер:
15.33 Mб
Скачать

Chapter 4

Visual Studio 2005

Visual Studio 2005 makes it rather easy and straightforward to create and configure data source controls and then bind them to any of the data-bound controls. So far in this chapter, you have looked at the creation of data source controls via the code that you write. This is the best way to learn about the capabilities of ASP.NET 2.0’s data controls. You can also take the approach of simply dragging and dropping these controls onto the Visual Studio 2005 design surface and manipulating the controls in the IDE’s Properties Window.

To create your data source controls and bind them to the data-bound controls, you can use the controls’ smart tags. For an example of this, drag and drop a GridView control onto an empty design surface. Then below the GridView control, drag and drop a SqlDataSource control onto the page.

The first step is to set up your SqlDataSource control to get the data you want. To do this in the visual designer, you simply highlight the gray box that represents the SqlDataSource control and click the arrow in the upper-right-hand corner of the control. Doing this opens the control’s smart tag and provides you with the option to configure and modify the control. The smart tag for the SqlDataSource control appears as shown in Figure 4-32.

Figure 4-32

Within the smart tag for the SqlDataSource control, click the Configure Data Source link. This initiates the Data Source Configuration Wizard. The first step in the configuration process is to select the database you are connecting to. The wizard then shows you the connection string it’s creating for you in a grayed-out pane (see Figure 4-33).

118

New Ways to Handle Data

Figure 4-33

When you click the Next button, the Data Source Configuration Wizard gives you the option of storing the connection string in the web.config file, which is an outstanding idea because it is a safe way to store it. Any .config file is not browser-accessible and gives you a single place in the application where you can change the connection string and have that change take effect throughout the application. Keep the check box for this checked if you want to store the connection string in this manner (see Figure 4-34); otherwise, uncheck the box.

119

Chapter 4

Figure 4-34

Again, click the Next button and configure the Select statement that the SqlDataSource control utilizes. Simply select the Customers table from the drop-down list and then check the * check box (see Figure 4-35). This means that you want each and every field from the Customers table.

120

New Ways to Handle Data

Figure 4-35

Click Next, and you can test out the SqlDataSource and its connection to the data store by clicking the Test Query button shown in Figure 4-36.

121

Chapter 4

Figure 4-36

Clicking the Finish button brings you back to the design surface in Visual Studio. The SqlDataSource control is now ready to use with the GridView control. In the GridView control, from the drop-down list in the smart tag, select the SqlDataSource1 control and check the Enable Paging check box (see Figure 4-37). Next, you can create a better look and feel by choosing the Auto Format option.

Figure 4-37

That’s it! Everything is in place and no coding is needed. Simply press F5 to run the page, and you get the results shown in Figure 4-38.

122