Scott Mitchell ASP.NET 2數據控件嵌套(3)_.Net教程
推薦:ASP.Net中無刷新執行Session身份驗證在寫一個客戶的B/S結構應用程序時,突然發現一個技巧,不知道是否是MS的一個BUG,給相關的有研究的朋友原先考慮寫一個檢查Session的類,Session失效后,必須轉向登陸頁面,可每一個調用該類的頁
下一步我們的任務是在CategoryList的ItemTemplate里添加一個Repeater用來顯示屬于各個category下的product.有很多方法可以存取內層的Repeater數據,我們將探討兩種現在我們在CategoryList Repeater的ItemTemplate里創建product Repeater.每個product里將包含name和price
我們將下面的標記加到CategoryList的ItemTemplate里:
ASP.NET | |
1 2 3 4 5 6 7 8 9 10 11 12 |
<asp:Repeater runat="server" ID="ProductsByCategoryList" EnableViewState="False"> <HeaderTemplate> <ul> </HeaderTemplate> <ItemTemplate> <li><strong><%# Eval("ProductName") %></strong> (<%# Eval("UnitPrice", "{0:C}") %>)</li> </ItemTemplate> <FooterTemplate> </ul> </FooterTemplate> </asp:Repeater> |
第三步: 將各Category下的Product綁定到 ProductsByCategoryList Repeater
如果現在你瀏覽這個頁,你會看到象圖4一樣的頁面,因為我們還沒有在Repeater里綁定任何數據.有幾種方法可以將合適的product記錄綁定到Repeater里,其中一些會比較有效.現在主要的任務是為指定category取到合適的product.
可以通過在ItemTemplate里語法聲明ObjectDataSource或者直接在后臺代碼編程來將數據綁定到內層的Repeater.
通過ObjectDataSource和ItemDataBound來獲取數據
這里我們還是用ObjectDataSource來實現.ProductsBLL類的GetProductsByCategoryID(Category)
方法可以返回特定CategoryID的products信息.因此,我們將在CategoryList Repeater的ItemTemplate里新建一個ObjectDataSource,并用這個方法配置它.
不幸的,Repeater不允許通過設計視圖來修改template,因此我們需要手動添加將聲明語法.見下面的代碼:
ASP.NET | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<h4><%# Eval("CategoryName") %></h4> <p><%# Eval("Description") %></p> <asp:Repeater runat="server" ID="ProductsByCategoryList" EnableViewState="False" DataSourceID="ProductsByCategoryDataSource"> <HeaderTemplate> <ul> </HeaderTemplate> <ItemTemplate> <li><strong><%# Eval("ProductName") %></strong> - sold as <%# Eval("QuantityPerUnit") %> at <%# Eval("UnitPrice", "{0:C}") %></li> </ItemTemplate> <FooterTemplate> </ul> </FooterTemplate> </asp:Repeater> <asp:ObjectDataSource ID="ProductsByCategoryDataSource" runat="server" SelectMethod="GetProductsByCategoryID" TypeName="ProductsBLL"> <SelectParameters> <asp:Parameter Name="CategoryID" Type="Int32" /> </SelectParameters> </asp:ObjectDataSource> |
當使用ObjectDataSource方法時我們需要設置ProductsByCategoryList Repeater的DataSourceID為ObjectDataSource(ProductsByCategoryDataSource).注意ObjectDataSource有一個<asp:Parameter>來指定傳給GetProductsByCategoryID(categoryID)的categoryID.但是我們怎么來指定這個值呢?我們可以設置DefaultValue屬性為<asp:Parameter>,見下面的代碼:
ASP.NET | |
1
|
<asp:Parameter Name="CategoryID" Type="Int32" DefaultValue='<%# Eval("CategoryID")' /> |
不幸的,數據綁定語法只能用在有DataBinding事件的控件里.Parameter類沒有這樣的事件,因此這樣使用會出錯.
我們需要為CategoryList Repeater的ItemDataBound創建一個事件處理來設置這個值.每個item綁定到Repeater時激發ItemDataBound事件.因此每次外層的Repeater激發這個時間時,我們可以將當前的CaegoryID的值傳給ProductsByCategoryDataSource ObjectDataSource的CategoryID參數.
下面的代碼是為CategoryList Repeater的ItemDataBound創建一個event handler:
C# | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
protected void CategoryList_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item) { // Reference the CategoriesRow object being bound to this RepeaterItem Northwind.CategoriesRow category = (Northwind.CategoriesRow)((System.Data.DataRowView)e.Item.DataItem).Row; // Reference the ProductsByCategoryDataSource ObjectDataSource ObjectDataSource ProductsByCategoryDataSource = (ObjectDataSource)e.Item.FindControl("ProductsByCategoryDataSource"); // Set the CategoryID Parameter value ProductsByCategoryDataSource.SelectParameters["CategoryID"].DefaultValue = category.CategoryID.ToString(); } } |
這個event handler首先保證我們操作的是data item而不是header,footer或separator item.然后,引用剛剛綁定到當前RepeaterItem的CategoriesRow實例.最后,引用在ItemTemplate里的ObjectDataSource并將當前RepeaterItem的CategoryID傳給CategoryID參數.
在這個event handler里,每個RepeaterItem里的ProductsByCategoryList Repeater都綁定到RepeaterItem的category里的product.見圖5.
分享:.net教程:ASP.NET GridView的分頁功能要實現GrdView分頁的功能。 操作如下: 1、更改GrdView控件的AllowPaging屬性為true。 2、更改GrdView控件的PageSize屬性為 任意數值(默認為10) 3、更改GrdView控件的PageSetting->Mod
- asp.net如何得到GRIDVIEW中某行某列值的方法
- .net SMTP發送Email實例(可帶附件)
- js實現廣告漂浮效果的小例子
- asp.net Repeater 數據綁定的具體實現
- Asp.Net 無刷新文件上傳并顯示進度條的實現方法及思路
- Asp.net獲取客戶端IP常見代碼存在的偽造IP問題探討
- VS2010 水晶報表的使用方法
- ASP.NET中操作SQL數據庫(連接字符串的配置及獲取)
- asp.net頁面傳值測試實例代碼
- DataGridView - DataGridViewCheckBoxCell的使用介紹
- asp.net中javascript的引用(直接引入和間接引入)
- 三層+存儲過程實現分頁示例代碼
- 相關鏈接:
- 教程說明:
.Net教程-Scott Mitchell ASP.NET 2數據控件嵌套(3)。