讀取數據
本章學習了 數據源和數據綁定控件,初步瞭解了ASP.Net數據源控件和數據綁定控件的一些用法,
以及數據源控件和數據綁定控件的連接和在數據綁定控件上對數據進行顯示。學到了怎樣有用TreeView
控件讀取XML數據文件。
數據源控件與數據綁定控件是相互配合的,要分清不同的數據源控件該使用哪些數據源(表還是樹),
如GridView就只能針對表,TreeView和Menu就只針對樹。
1.數據源控件:
數據源控件代碼:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="./SQLEXPRESS;
AttachDbFilename=C:/WroxUnited.mdf;
Intergrated Security=True;
User Instance=True"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT * FROM [Fixtures]">
</asp:SqlDataSource>
數據庫的名稱用 AttachDbFileName決定。 Intergrated Security表明用Windows Authentication.
Provider設置提供商。如果想隱藏連接字符串,可把連接字符串寫在 Web.config文件裏。
數據源控件代碼:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ConnectionStrings:WroxUnited%>"
SelectCommand="SELECT * FROM [Fixtures]"
</asp:SqlDataSource>
Web.config文件裏代碼:
<ConnectionStrings>
<add name="WroxUnited"
ConnectionString="./SQLEXPRESS;
AttachDbFilename=|DataDirectory|WroxUnited.mdf;
Intergrated Security=True;
Connect Timeout=30;
User Instance=True"
ProviderName="System.Data.SqlClient"
/>
</ConnectionStrings>
2.數據綁定控件:
分類:*選擇列表控件:GridView,DataList,Repeater. 注:所有選擇列表控件都有一個重要的屬性,
AutoPostBack將在列表中的選擇發生改變
時自動回送到服務器。
*顯示單條記錄的控件:DetailsView,FormView.
*用於層次結構數據的樹型控件。
多個數據控件可以一起工作,如在GridView中選擇了一條記錄,需要把這條記錄詳細的顯示在DetailsView
裏,那麼就需要在兩個數據控件之間設置參數。這個在VWD中可以根據嚮導來做!
最後介紹了TreeView對XML數據的讀取。將TreeView的數據源選擇爲XML文件即可。
綁定語法:
<asp:DataList ID="DataList1" runat="server"
DataSourceID="SqlDataSource1"
<ItemTemplate>
<asp:Label ID="FixtureIDLabel"
runat="server"
Text='<%# EVAL("FixtuerID")%>'
BackColor="Yellow"></asp:Label>
>
</ItemTemplate>
<asp:DataList>
</asp:DataList>