Scott Mitchell ASP.NET 2數據控件嵌套(3)_.Net教程

      編輯Tag賺U幣
      教程Tag:暫無Tag,歡迎添加,賺取U幣!

      推薦: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

      來源:模板無憂//所屬分類:.Net教程/更新時間:2008-08-22
      相關.Net教程