Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ASP .NET Database Programming Weekend Crash Course - J. Butler, T. Caudill.pdf
Скачиваний:
31
Добавлен:
24.05.2014
Размер:
3.32 Mб
Скачать

226

Sunday Morning

Finally, add two new attributes, shown in bold below, to the DropDownList server control so that you can select which of the class properties provides the dataTextField and dataValueField attributes:

<ASP:DROPDOWNLIST ID=”dBox1” RUNAT=”Server”

DATATEXTFIELD=”ProductTitle”

DATAVALUEFIELD=”ProductId”

ONSELECTEDINDEXCHANGED=”dBox1_SelectedIndexChanged” AUTOPOSTBACK=”true”

/>

CD-ROM

The full code for the classbinding example in this section can be found in the Session 22 folder on the CD-ROM, using the filename dropdownbindtoobjectarray.aspx.

In the following sections, you will see how the data binding functionality can be extended to include database tables, views, stored procedures and XML datasets.

Binding to Database Data

The examples in this section require that you have the pubs database installed and available.

Note

Binding a control to a database table, view or stored procedure is very straightforward. First, you have to make sure you import the System.Data and System.Data.OleDb namespaces so that you have access to the ADO.NET objects as shown in Listing 22-3:

Listing 22-3 Example of binding server controls to database

<%@ Page Language=”VB” Debug=”False” %> <%@ Import Namespace=”System.Data” %>

<%@ Import Namespace=”System.Data.OleDB” %>

Then implement a connection to the database in the Page_Load () event:

<HTML>

<SCRIPT LANGUAGE=”VB” RUNAT=”Server”>

Sub Page_Load(Sender as Object, E as EventArgs) If Not IsPostback Then

Dim connection as New OleDBConnection(“provider=sqloledb;Data Source=(local);Initial Catalog=pubs;User ID=sa;pwd=;”)

In this case, we are connecting to the Pubs database on the local machine. Next, use the OleDBDataAdapter and DataSet objects you explored in earlier sessions to get the required dataset from the Authors table:

Session 22—Introducing Data Binding

227

Dim myAdapter as New OleDBDataAdapter(“SELECT * FROM Authors”, connection) Dim myDataset As New DataSet()

myAdapter.Fill(myDataset, “myDataset”)

Then, just as we did in the array examples earlier, you need to set the DataSource properties on the server control and then call the DataBind() method.

dbox1.DataSource = MyDataset.Tables(0).DefaultView dbox1.DataBind()

End If End Sub </SCRIPT>

Finally, you make sure that you have set the appropriate dataTextField and dataValueField values on the server control:

<BODY>

<FORM RUNAT=”Server” METHOD=”post” ID=”Form1”>

<ASP:DROPDOWNLIST ID=”dBox1” RUNAT=”Server” DATATEXTFIELD=”au_lname” DATAVALUEFIELD=”au_id” AUTOPOSTBACK=”true” />

</FORM>

</BODY>

</HTML>

You can use the same approach to bind server controls to stored procedures and views by modifying the DataAdapter properties as required.

Binding to XML

Binding server controls to an XML file is not any more complicated than binding using a standard database. Basically, you simply replace the Connection and DataAdapter objects with a FileStream object and a StreamReader object. The following example shows how to bind a simple XML dataset to a dropdown control.

You need to import the System.IO namespace in addition to System.Data so that you can open and read physical files. This will allow you to open and read an xml file stored in a local directory.

<%@ Page Language=”VB” Debug=”True” Trace=”False”%> <%@ Import Namespace=”System.IO”%>

<%@ Import Namespace=”System.Data” %> <HTML>

<SCRIPT LANGUAGE=”VB” RUNAT=”Server”>

Next, you need to open the XML file. In this example, you have an XML representation of the Authors table from the Pubs database stored as pubs_authors.xml on your CD-ROM in the Session 22 folder.

Sub Page_Load(Sender as Object, E as EventArgs)

Dim fs As New FileStream(Server.MapPath(“pubs_authors.xml”), FileMode.Open,

FileAccess.Read)

Once you have the file open, you can store its contents in a StreamReader object.

Dim xmlstream as New StreamReader(fs)