濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > 在ASP.NET 2.0中操作數(shù)據(jù)之四十五:DataList和Repeater里的自定義Button

在ASP.NET 2.0中操作數(shù)據(jù)之四十五:DataList和Repeater里的自定義Button

熱門標(biāo)簽:臨沂智能電話機(jī)器人加盟 400電話辦理怎么樣 聯(lián)通官網(wǎng)400電話辦理 西寧呼叫中心外呼系統(tǒng)線路商 蘇州如何辦理400電話 地圖標(biāo)注軟件免費(fèi)下載 百應(yīng)電話機(jī)器人外呼系統(tǒng) 外呼電話機(jī)器人成本 網(wǎng)絡(luò)電話外呼系統(tǒng)上海

導(dǎo)言

  在前面關(guān)于DataList 和Repeater 的7章教程里,我們分別創(chuàng)建了只讀和可以編輯刪除的例子。為了讓DataList有編輯和刪除的功能,我們?cè)贗temTemplate里添加了一些button,當(dāng)點(diǎn)擊時(shí),引起postback,并根據(jù)button的CommandName屬性激發(fā)相關(guān)的事件。例如,添加一個(gè)CommandName為“Edit”的button,在postback時(shí)會(huì)激發(fā)EditCommand事件,如果CommandName為“Delete”則激發(fā)DeleteCommand。

  除了編輯和刪除button,DataList和Repeater還可以包含一些當(dāng)點(diǎn)擊時(shí),執(zhí)行自定義服務(wù)器端邏輯的Button,LinkButton和ImageButton。本章我們將創(chuàng)建一個(gè)在Repeater里列出categories的界面。每個(gè)category都包含一個(gè)button,當(dāng)點(diǎn)擊時(shí)會(huì)列出相關(guān)product。見(jiàn)圖1。


圖 1: 點(diǎn) “Show Products” 顯示目錄下所有product

第一步: 添加教程頁(yè)

首先添加本章需要的頁(yè)。添加一個(gè)名為CustomButtonsDataListRepeater的文件夾。然后添加下面兩個(gè)頁(yè),記得包含Site.master母板頁(yè)。

    Default.aspx
    CustomButtons.aspx


圖 2: 添加頁(yè)

和其它文件夾一樣,CustomButtonsDataListRepeater文件夾下的Default.aspx頁(yè)會(huì)列出本部分的教程。和前面一樣添加SectionLevelTutorialListing.ascx用戶控件。


圖 3: 添加 SectionLevelTutorialListing.ascx用戶控件

最后,在Web.sitemap里添加這些頁(yè)的信息。見(jiàn)下面的標(biāo)記:

siteMapNode
 url="~/CustomButtonsDataListRepeater/Default.aspx"
 title="Adding Custom Buttons to the DataList and Repeater"
 description="Samples of DataList and Repeater Reports that Include
     Buttons for Performing Server-Side Actions">
 siteMapNode
  url="~/CustomButtonsDataListRepeater/CustomButtons.aspx"
  title="Using Custom Buttons in the DataList and Repeater's Templates"
  description="Examines how to add custom Buttons, LinkButtons,
      or ImageButtons within templates." />
/siteMapNode>

完成后瀏覽該頁(yè)。見(jiàn)圖4。


圖 4: 現(xiàn)在的站點(diǎn)地圖包含了本章的頁(yè)

第二步: 添加 Categories列表

  我們需要添加一個(gè)列出所有categories,每個(gè)category都有一個(gè)“Show Products” LinkButton的Repeater。點(diǎn)LinkButton時(shí)會(huì)顯示所有category相關(guān)的products。我們首先創(chuàng)建一個(gè)列出所有categories的Repeater。打開(kāi)CustomButtons.aspx頁(yè),拖一個(gè)Repeater進(jìn)來(lái),將ID設(shè)為Categories。然后從智能標(biāo)簽里創(chuàng)建一個(gè)名為CategoriesDataSource的ObjectDataSource,用CategoriesBLL類的GetCategories()方法配置它。


圖5: 配置ObjectDataSource

  Visual Studio會(huì)根據(jù)數(shù)據(jù)源為DataList創(chuàng)建一個(gè)默認(rèn)的ItemTemplate,而Repeater的templates需要手工定義。而且Repeater的templates需要直接通過(guò)聲明代碼來(lái)創(chuàng)建和修改(也就是說(shuō)在智能標(biāo)簽上沒(méi)有“Edit Templates”選項(xiàng))

  點(diǎn)左下角的源視圖,添加一個(gè)以h3>顯示category name,以段落description的ItemTemplate。并包含一個(gè)在每個(gè)category之間顯示水平線(hr />)的SeparatorTemplate。同樣還要添加一個(gè)LinkButton,將Text設(shè)為“Show Products”。完成這些后你的頁(yè)面聲明代碼應(yīng)該和下面差不多:

asp:Repeater ID="Categories" DataSourceID="CategoriesDataSource"
 runat="server">
 ItemTemplate>
  h3>%# Eval("CategoryName") %>/h3>
  p>
   %# Eval("Description") %>
   [asp:LinkButton runat="server" ID="ShowProducts">
    Show Products/asp:LinkButton>]
  /p>
 /ItemTemplate>
 SeparatorTemplate>hr />/SeparatorTemplate>
/asp:Repeater>
asp:ObjectDataSource ID="CategoriesDataSource" runat="server"
 OldValuesParameterFormatString="original_{0}"
 SelectMethod="GetCategories" TypeName="CategoriesBLL">
/asp:ObjectDataSource>

  圖6是瀏覽該頁(yè)的樣子。每個(gè)category name和description都被列出來(lái)。當(dāng)點(diǎn)“Show Products” button時(shí)會(huì)引起postback,但是還不執(zhí)行任何功能。


圖 6: 每個(gè) Category'的Name 和 Description 和 “Show Products” LinkButton一起列出

第三步:當(dāng)點(diǎn)“Show Products” LinkButton 時(shí)執(zhí)行服務(wù)器端代碼

  任何時(shí)候,當(dāng)DataList或Repeater的template里的Button, LinkButton, ImageButton被點(diǎn)時(shí),會(huì)產(chǎn)生postback,并激發(fā)DataList或Repeater的ItemCommand事件。除了ItemCommand外,如果button'的CommandName 設(shè)為(“Delete”, “Edit”, “Cancel”, “Update”,  “Select”)其中一個(gè)時(shí),DataList會(huì)激發(fā)另外一個(gè)事件。但是ItemCommand是都會(huì)激發(fā)的。

  當(dāng)DataList或Repeater的template里的Button被點(diǎn)時(shí),通常我們需要獲取哪個(gè)button被點(diǎn)了(一個(gè)控件里可能有多個(gè)button,比如編輯和刪除),還可能需要一些其它的信息(比如那些button被點(diǎn)的item(項(xiàng))的主鍵)。Button, LinkButton, ImageButton提供了兩個(gè)屬性,它們的值可以傳給ItemCommand event handler:

    CommandName –表示template里每個(gè)button身份的字符串 。
    CommandArgument – 通常用來(lái)保存一些值,比如主鍵。

  在這個(gè)例子里,將LinkButton的CommandName設(shè)為“ShowProducts”,并將當(dāng)前記錄的主鍵– CategoryID –通過(guò)綁定語(yǔ)法綁定到CommandArgument(CategoryArgument='%# Eval("CategoryID") %>')。完成這些后,LinkButton的聲明語(yǔ)法看起來(lái)應(yīng)該和下面差不多:

asp:LinkButton runat="server" CommandName="ShowProducts"
 CommandArgument='%# Eval("CategoryID") %>' ID="ShowProducts">
 Show Products/asp:LinkButton>

  當(dāng)button被點(diǎn)時(shí),產(chǎn)生postback并激發(fā)DataList或Repeater的ItemCommand事件。Button的CommandName和CommandArgument值被傳到event handler里。

  為ItemCommand事件創(chuàng)建一個(gè)event handler,注意event handler的第二個(gè)參數(shù)(名字為e)。這個(gè)參數(shù)的類型為RepeaterCommandEventArgs,它有以下4個(gè)屬性:

    CommandArgument – 被點(diǎn)的 button'的CommandArgument property 的值
    CommandName –  button'的CommandName property 的值
    CommandSource – 被點(diǎn) button 的引用
    Item – 包含被點(diǎn)button 的 RepeaterItem的引用; 每條綁定到Repeater的記錄被表明為一個(gè) RepeaterItem

  由于選擇的category的CategoryID通過(guò)CommandArgument傳入,我們可以在ItemCommand event handler里獲取與之相關(guān)的products。這些products在ItemTemplate(我們已經(jīng)添加過(guò)了)里綁定到一個(gè)BulletedList。剩下的事就是添加BulletedList,在ItemCommand event handler里引用它,然后將選擇的category的products綁定到BulletedList,我們將在第四步完成這個(gè)。

  注意:DataList的ItemCommand event handler傳入了一個(gè)DataListCommandEventArgs類型的對(duì)象,它提供和RepeaterCommandEventArgs 一樣的4個(gè)屬性。

第四步: 顯示選擇的Category的 Products

  在ItemTemplate里顯示products可以使用很多控件,我們可以添加一個(gè)嵌套的Repeater,DataList,DropDownList,GridView等。在這里我們使用BulletedList?;氐紺ustomButtons.aspx page頁(yè)的聲明代碼,在“Show Products” LinkButton后添加一個(gè)BulletedList。將ID設(shè)為ProductsInCategory。BulletedList顯示那些通過(guò)DataTextField屬性指定的字段值。由于將有product信息綁定到這個(gè)屬性,我們將DataTextField設(shè)為ProductName。

asp:BulletedList ID="ProductsInCategory" DataTextField="ProductName"
 runat="server">/asp:BulletedList>

在ItemCommand event handler里通過(guò)e.Item.FindControl("ProductsInCategory")引用這個(gè)控件,并與products綁定。

protected void Categories_ItemCommand(object source, RepeaterCommandEventArgs e)
{
 if (e.CommandName == "ShowProducts")
 {
  // Determine the CategoryID
  int categoryID = Convert.ToInt32(e.CommandArgument);
  // Get the associated products from the ProudctsBLL and bind
  // them to the BulletedList
  BulletedList products =
   (BulletedList)e.Item.FindControl("ProductsInCategory");
  ProductsBLL productsAPI = new ProductsBLL();
  products.DataSource =
   productsAPI.GetProductsByCategoryID(categoryID);
  products.DataBind());
 }
}

  在ItemCommand event handler里執(zhí)行任何操作前,需要先檢查傳入的CommandName。由于ItemCommand event handler在任何button被點(diǎn)時(shí)都會(huì)執(zhí)行,如果在template里有多個(gè)button時(shí)需要通過(guò)CommandName的值來(lái)辨別需要采取什么操作。由于我們這里只有一個(gè)button,因此在這里檢查CommandName是沒(méi)意義的,但是這是一個(gè)好習(xí)慣。然后,選擇的category的CategoryID通過(guò)CommandArgument獲取。然后引用Template里的BulletedList并綁定ProductsBLL類的GetProductsByCategoryID(categoryID)方法的結(jié)果。

  在前面DataList里使用button的教程里,比如在DataList里編輯和刪除數(shù)據(jù)概述,我們通過(guò)DataKeys集合來(lái)獲取給定item的主鍵。這個(gè)方法在DataList里很好用,但是Repeater沒(méi)有DataKeys屬性。因此我們需要換一種方法來(lái)提供主鍵的值,比如通過(guò)button的 CommandArgument,或者在template使用一個(gè)隱藏的Label,然后通過(guò)e.Item.FindControl("LabelID")在ItemCommand event handler里讀出它的值。

  完成ItemCommand event handler后,瀏覽該頁(yè)。見(jiàn)圖7。點(diǎn)“Show Products” link會(huì)引起postback,并顯示相關(guān)的products。而且,注意當(dāng)點(diǎn)其它“Show Products” links時(shí)前面的product信息會(huì)保留。

  注意:如果你需要修改這個(gè)報(bào)表的行為,比如一次只列出一個(gè)category的products,僅僅只需要將BulletedList的EnableViewState屬性設(shè)為False。


圖 7: 用 BulletedList 顯示選擇Category關(guān)聯(lián)的 Products.

總結(jié)

  DataList和Repeater可以在templates里包含很多Buttons, LinkButtons,  ImageButtons。這些button被點(diǎn)時(shí)會(huì)引起postback,并激發(fā)ItemCommand事件。為ItemCommand event.創(chuàng)建一個(gè)event handler可以將服務(wù)器端代碼和點(diǎn)擊button關(guān)聯(lián)起來(lái)。在這個(gè)event handler里首先檢查傳入的CommandName的值來(lái)判斷是哪個(gè)button被點(diǎn)了。其它另外的信息可以通過(guò)CommandArgument屬性來(lái)提供。

  祝編程快樂(lè)!

作者簡(jiǎn)介

  本系列教程作者 Scott Mitchell,著有六本ASP/ASP.NET方面的書,是4GuysFromRolla.com的創(chuàng)始人,自1998年以來(lái)一直應(yīng)用 微軟Web技術(shù)。大家可以點(diǎn)擊查看全部教程《[翻譯]Scott Mitchell 的ASP.NET 2.0數(shù)據(jù)教程》,希望對(duì)大家的學(xué)習(xí)ASP.NET有所幫助。

您可能感興趣的文章:
  • 讓W(xué)in2008+IIS7+ASP.NET支持10萬(wàn)并發(fā)請(qǐng)求
  • c#實(shí)現(xiàn)服務(wù)器性能監(jiān)控并發(fā)送郵件保存日志
  • C#線程執(zhí)行超時(shí)處理與并發(fā)線程數(shù)控制實(shí)例
  • c#編寫的高并發(fā)數(shù)據(jù)庫(kù)控制訪問(wèn)代碼
  • C#使用隊(duì)列(Queue)解決簡(jiǎn)單的并發(fā)問(wèn)題
  • 在ASP.NET 2.0中操作數(shù)據(jù)之二十一:實(shí)現(xiàn)開(kāi)放式并發(fā)
  • 在ASP.NET 2.0中操作數(shù)據(jù)之四十四:DataList和Repeater數(shù)據(jù)排序(三)
  • 在ASP.NET 2.0中操作數(shù)據(jù)之四十六:使用SqlDataSource控件檢索數(shù)據(jù)
  • 在ASP.NET 2.0中操作數(shù)據(jù)之四十七:用SqlDataSource控件插入、更新、刪除數(shù)據(jù)
  • 在ASP.NET 2.0中操作數(shù)據(jù)之四十八:對(duì)SqlDataSource控件使用開(kāi)放式并發(fā)

標(biāo)簽:中衛(wèi) 清遠(yuǎn) 甘肅 慶陽(yáng) 臨夏 聊城 海西

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《在ASP.NET 2.0中操作數(shù)據(jù)之四十五:DataList和Repeater里的自定義Button》,本文關(guān)鍵詞  在,ASP.NET,2.0,中,操作,數(shù)據(jù),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《在ASP.NET 2.0中操作數(shù)據(jù)之四十五:DataList和Repeater里的自定義Button》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于在ASP.NET 2.0中操作數(shù)據(jù)之四十五:DataList和Repeater里的自定義Button的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    武安市| 镇原县| 枣阳市| 防城港市| 崇义县| 当阳市| 汝阳县| 即墨市| 隆化县| 喀喇| 邵东县| 固安县| 浦北县| 商都县| 垫江县| 湾仔区| 安义县| 屏边| 岳西县| 涡阳县| 博罗县| 北海市| 郸城县| 自治县| 花莲市| 聂拉木县| 香港 | 梅州市| 延边| 桐城市| 休宁县| 霍州市| 海原县| 原阳县| 台江县| 武鸣县| 英超| 简阳市| 福州市| 兴宁市| 会昌县|