Visual Studio 2008中綁定ListView控件
ListView 是Visual Studio 2008中一個新的控件,使用此控件可以很輕松的進(jìn)行數(shù)據(jù)的插入、編輯、刪除和排序,也可以在使用這個控件時通過用戶定義模板來靈活地顯示各種格式的數(shù)據(jù)。
下面的如何將ListView 綁定到一個SQLDataSource(sql數(shù)據(jù)源) 步驟:
1、從工具箱的數(shù)據(jù)標(biāo)簽里添加一個Listview控件, Visual Studio將會生成如下圖所示的代碼
2、為這個控件綁定一個SQLDataSource,在這里,我們復(fù)制一個NorthWind.mdf 數(shù)據(jù)庫到網(wǎng)站項目的 App_Data文件夾。
3、配置這個SQLDataSource使用“類別”表,將產(chǎn)生如下一段代碼:
- < span style="color: #800000;">< /span> ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
- < span style="color: #800000;">< /span> ID="SqlDataSource1" runat="server"
- ConnectionString=""
- SelectCommand="SELECT * FROM [Categories]">
4、打開LIstview控件的智能標(biāo)記,選擇“配置ListView”打開“配置ListView”對話框
5、在這個配置對話框里,你可以選擇布局和預(yù)定義的樣式,比如選擇“grid”和“ Professional”,如果你想分頁的話,你可以選中“啟動分頁”選項。
6、在源碼視圖會產(chǎn)生如下代碼:
- < span style="color: #800000;">< /span>
- < table id="Table1" border="0">
- < tbody>< tr id="Tr1">
- < td id="Td1">
- < table id="itemPlaceholderContainer" style="background-color: #FFFFFF;border-collapse: collapse;" border="1">
- < tbody>< tr id="Tr2" style="background-color:#DCDCDC;color: #000000;">
- < th id="Th1">
- CategoryID< /th>
- < th id="Th2">
- CategoryName< /th>
- < th id="Th3">
- Description< /th>
- < th id="Th4">
- Picture< /th>
- < /tr>
- < tr id="itemPlaceholder">
- < /tr>
- < /tbody>< /table>
- < /td>
- < /tr>
- < tr id="Tr3">
- < td id="Td2" style="text-align: center;background-color: #CCCCCC;font-family: Verdana, Helvetica,#000000;">
- < strong>< span style="color: #800000;">< /span>< /strong> ID="DataPager1" runat="server">
- < strong>< /strong>
- < /td>
- < /tr>
- < /tbody>< /table>
- view plaincopy to clipboardprint?
- < span style="color: #800000;">< /span>
7、運行這個頁面,將會顯示一個帶分頁的的(數(shù)據(jù)展示)頁面。
這里只是介紹的一個非常簡單的listview綁定的例子。
ListView 控件是net Framework3.5里的控件,如果在你的項目是基于net Framework2.0或3.0 ,在工具箱是無法看見這個控件。這個控件和Gridview控件是在Asp.net中最常用的數(shù)據(jù)控件。在顯示不是很復(fù)雜的數(shù)據(jù)方面很有優(yōu)勢,甚至比Gridview更靈活適用,它不像Gridview那樣不是行字段來顯示數(shù)據(jù),而是使用用戶定義的模板。
個人覺得應(yīng)該要很好的利用ListView,而現(xiàn)在Asp.net的開發(fā)對GridView有濫用的傾向。
【編輯推薦】