日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

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

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

推薦:ASP.Net中無(wú)刷新執(zhí)行Session身份驗(yàn)證
在寫(xiě)一個(gè)客戶的B/S結(jié)構(gòu)應(yīng)用程序時(shí),突然發(fā)現(xiàn)一個(gè)技巧,不知道是否是MS的一個(gè)BUG,給相關(guān)的有研究的朋友原先考慮寫(xiě)一個(gè)檢查Session的類,Session失效后,必須轉(zhuǎn)向登陸頁(yè)面,可每一個(gè)調(diào)用該類的頁(yè)


下一步我們的任務(wù)是在CategoryList的ItemTemplate里添加一個(gè)Repeater用來(lái)顯示屬于各個(gè)category下的product.有很多方法可以存取內(nèi)層的Repeater數(shù)據(jù),我們將探討兩種現(xiàn)在我們?cè)贑ategoryList Repeater的ItemTemplate里創(chuàng)建product Repeater.每個(gè)product里將包含name和price

我們將下面的標(biāo)記加到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

如果現(xiàn)在你瀏覽這個(gè)頁(yè),你會(huì)看到象圖4一樣的頁(yè)面,因?yàn)槲覀冞沒(méi)有在Repeater里綁定任何數(shù)據(jù).有幾種方法可以將合適的product記錄綁定到Repeater里,其中一些會(huì)比較有效.現(xiàn)在主要的任務(wù)是為指定category取到合適的product.

可以通過(guò)在ItemTemplate里語(yǔ)法聲明ObjectDataSource或者直接在后臺(tái)代碼編程來(lái)將數(shù)據(jù)綁定到內(nèi)層的Repeater.

通過(guò)ObjectDataSource和ItemDataBound來(lái)獲取數(shù)據(jù)

這里我們還是用ObjectDataSource來(lái)實(shí)現(xiàn).ProductsBLL類的GetProductsByCategoryID(Category)
方法可以返回特定CategoryID的products信息.因此,我們將在CategoryList Repeater的ItemTemplate里新建一個(gè)ObjectDataSource,并用這個(gè)方法配置它.

不幸的,Repeater不允許通過(guò)設(shè)計(jì)視圖來(lái)修改template,因此我們需要手動(dòng)添加將聲明語(yǔ)法.見(jiàn)下面的代碼:

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>

            


當(dāng)使用ObjectDataSource方法時(shí)我們需要設(shè)置ProductsByCategoryList Repeater的DataSourceID為ObjectDataSource(ProductsByCategoryDataSource).注意ObjectDataSource有一個(gè)<asp:Parameter>來(lái)指定傳給GetProductsByCategoryID(categoryID)的categoryID.但是我們?cè)趺磥?lái)指定這個(gè)值呢?我們可以設(shè)置DefaultValue屬性為<asp:Parameter>,見(jiàn)下面的代碼:
ASP.NET
1

            
<asp:Parameter Name="CategoryID" Type="Int32" DefaultValue='<%# Eval("CategoryID")' />

            

不幸的,數(shù)據(jù)綁定語(yǔ)法只能用在有DataBinding事件的控件里.Parameter類沒(méi)有這樣的事件,因此這樣使用會(huì)出錯(cuò).


我們需要為CategoryList Repeater的ItemDataBound創(chuàng)建一個(gè)事件處理來(lái)設(shè)置這個(gè)值.每個(gè)item綁定到Repeater時(shí)激發(fā)ItemDataBound事件.因此每次外層的Repeater激發(fā)這個(gè)時(shí)間時(shí),我們可以將當(dāng)前的CaegoryID的值傳給ProductsByCategoryDataSource ObjectDataSource的CategoryID參數(shù).

下面的代碼是為CategoryList Repeater的ItemDataBound創(chuàng)建一個(gè)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();

            }

            }

            

這個(gè)event handler首先保證我們操作的是data item而不是header,footer或separator item.然后,引用剛剛綁定到當(dāng)前RepeaterItem的CategoriesRow實(shí)例.最后,引用在ItemTemplate里的ObjectDataSource并將當(dāng)前RepeaterItem的CategoryID傳給CategoryID參數(shù).

在這個(gè)event handler里,每個(gè)RepeaterItem里的ProductsByCategoryList Repeater都綁定到RepeaterItem的category里的product.見(jiàn)圖5.

分享:.net教程:ASP.NET GridView的分頁(yè)功能
要實(shí)現(xiàn)GrdView分頁(yè)的功能。 操作如下: 1、更改GrdView控件的AllowPaging屬性為true。 2、更改GrdView控件的PageSize屬性為 任意數(shù)值(默認(rèn)為10) 3、更改GrdView控件的PageSetting->Mod

來(lái)源:模板無(wú)憂//所屬分類:.Net教程/更新時(shí)間:2008-08-22
相關(guān).Net教程