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

228

Sunday Morning

You then can fill your DataSet object using the DataSet.ReadXML() method and passing in your StreamReader. It is important that you remember to close the FileStream object when you are done, as it isn’t closed automatically:

Dim ds as New DataSet

If Not IsPostback Then ds.ReadXML(xmlStream) fs.Close

From here on out, you handle everything exactly as you did in the database example, setting the DataSource and calling the DataBind() method:

dbox1.DataSource = ds.Tables(0) dbox1.DataBind()

End If End Sub

Sub dBox1_SelectedIndexChanged(sender As Object , e As System.EventArgs) Response.Write (dbox1.SelectedItem.Value.ToString())

End Sub </SCRIPT> <BODY>

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

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

</FORM>

</BODY>

</HTML>

The above code sample is stored in your Session 22 folder on the CD as dropdownbindtoxml.aspx. Next, we will look at what’s required to bind one of the more advanced controls, the TreeView server control.

TreeView Control

The TreeView control is a supplemental control provided by Microsoft separately from the .NET Framework. It is one of four controls that are provided by Microsoft specifically for Web development purposes. The other ASP.NET Web controls include a Toolbar, TabControl and Multipage controls. These controls are made available at http://msdn.microsoft.com/ downloads/samples/Internet/ASP_DOT_NET_ServerControls/WebControls/sample.asp. You will need to download and install these controls in order to run the samples in this section.

The TreeView control provides an excellent opportunity to illustrate how you can use the flexibility of XML combined with the TreeView control to support a robust navigational framework for your Web site. In Figure 22-1, you can see the use of the TreeView control to provide navigation for an online directory.

Session 22—Introducing Data Binding

229

Figure 22-1 TreeView control bound to an XML File

The TreeView control enables you to develop a hierarchical navigational framework to represent any parent-child relationship. The hierarchical relationship shown in Figure 22-1 is stored in an xml file called treeview.xml.

Implement the TreeView server control

The data binding for the TreeView control is initiated through the TreeNodeSrc attribute. Here is an excerpt of Listing 22-4, showing the TreeNodeSrc attribute of the TreeView server control:

<IE:TREEVIEW RUNAT=”server” AUTOPOSTBACK=true ID=tvw1 CHILDTYPE=”Folder” SHOWPLUS=”True”

TREENODESRC=”treeview.xml”>

This statement will bind the treeview control to the treeview.xml file. The treeview.xml file must support a specific hierarchical format as demonstrated below:

<TREENODES>

<TREENODE Text=”Shopping”>

<TREENODE Text=”The Gap” NavigateURL=”http://www.gap.com” Target=”main”/> <TREENODE Text=”Brooks Brothers” Type=”item”

NavigateURL=”http://www.brooksbrothers.com” Target=”main”/> </TREENODE>

<TREENODE Text=”Restaurants”>

<TREENODE Text=”Chianti” Type=”item”/> <TREENODE Text=”Vinces” Type=”treeview.xml”/>

</TREENODE>

<TREENODE Text=”Books”>

<TREENODE Text=”ASP.Net Books” Target=””/>

<TREENODE Text=”Hungry Minds” NavigateURL=”http://www.hungryminds.com” Target=”main”/>

<TREENODE Text=”Amazon.com” NavigateURL=”http://www.amazon.com” Target=”main”/> </TREENODE>

</TREENODES>