鍍金池/ 教程/ C#/ ASP.NET - 數(shù)據(jù)綁定
調(diào)試
管理狀態(tài)
Panel 控件
Web 服務(wù)
語言集成查詢
數(shù)據(jù)源
基礎(chǔ)控件
廣告輪轉(zhuǎn)器
服務(wù)器端
服務(wù)器控件
ADO.NET
多線程
HTML 服務(wù)器
生命周期
Ajax 控制
客戶端
異常處理
環(huán)境設(shè)置
部署
個(gè)性化
驗(yàn)證器
多視圖
日歷
文件上傳
ASP.NET - 數(shù)據(jù)綁定
數(shù)據(jù)庫存取
實(shí)例
自定義控件
簡(jiǎn)介
配置
數(shù)據(jù)緩存
安全性
指令
事件處理

ASP.NET - 數(shù)據(jù)綁定

每一個(gè) ASP.NET 網(wǎng)頁表單控件從它的父控件類繼承了 DataBind 方法,它給予了它繼承的能力來綁定數(shù)據(jù)到它屬性中的至少一個(gè)屬性。這就是所謂的簡(jiǎn)單數(shù)據(jù)綁定或者內(nèi)部數(shù)據(jù)綁定。

簡(jiǎn)單數(shù)據(jù)綁定包括將任何實(shí)現(xiàn) IEnumerable 接口的集合(項(xiàng)目集合),或者 DataSet 和 DataTable 類附加到控件的 DataSource 屬性。

另一方面,一些控件可以通過 DataSource 控件綁定記錄,列表,或者數(shù)據(jù)列到它們的結(jié)構(gòu)中。這些控件源自 BaseDataBoundControl 類。這被叫做描述性數(shù)據(jù)綁定

data source 控件幫助 data-bound 控件實(shí)現(xiàn)了比如排序,分頁和編輯數(shù)據(jù)集合的功能。

BaseDataBoundControl 是一個(gè)抽象類,它通過兩個(gè)抽象類繼承:

  • DataBoundControl
  • HierarchicalDataBoundControl

抽象類 DataBoundControl 也由兩個(gè)抽象類繼承:

  • ListControl
  • CompositeDataBoundControl

能夠簡(jiǎn)單綁定數(shù)據(jù)的控件源自 ListControl 抽象類并且這些控件是:

  • BulletedList
  • CheckBoxList
  • DropDownList
  • ListBox
  • RadioButtonList

能夠描述性數(shù)據(jù)綁定的控件(一個(gè)更復(fù)雜的數(shù)據(jù)綁定)源自抽象類 CompositeDataBoundControl。這是控件是:

  • DetailsView
  • FormView
  • GridView
  • RecordList

簡(jiǎn)單數(shù)據(jù)綁定

簡(jiǎn)單數(shù)據(jù)綁定包括只讀選擇列表。這些控件能綁定一個(gè)數(shù)組列或者數(shù)據(jù)庫的字段。選擇列表從數(shù)據(jù)庫中或 data source 中取兩個(gè)值;一個(gè)值用過列表表示而另一個(gè)被認(rèn)為是相應(yīng)顯示的值。

讓我們使用一個(gè)小例子來理解這個(gè)概念。用一個(gè)項(xiàng)目符號(hào)列表和一個(gè) SqlDataSource 控件來創(chuàng)建一個(gè)網(wǎng)頁。配置 data source 控件來從你的數(shù)據(jù)庫中(我們?cè)谥暗恼鹿?jié)中使用相同的 DotNetReferences 表)檢索兩個(gè)值。

為包含的項(xiàng)目符號(hào)列表控件選擇一個(gè) data source:

  • 選擇 data source 控件
  • 選擇一個(gè)字段來展示,它被叫做數(shù)據(jù)字段
  • 選擇值的字段

http://wiki.jikexueyuan.com/project/asp-net/images/choose_data_source.jpg" alt="1" />

當(dāng)應(yīng)用程序執(zhí)行的時(shí)候,檢查整個(gè)標(biāo)題列綁定到項(xiàng)目符號(hào)列表并被展示。

http://wiki.jikexueyuan.com/project/asp-net/images/choose_data_source2.jpg" alt="2" />

描述性數(shù)據(jù)綁定

我們已經(jīng)在之前的指南中使用 GridView 控件來使用描述性數(shù)據(jù)綁定。其他復(fù)合的能夠以表格的方式展示并操作數(shù)據(jù)的 data bound 控件是 DetailsView, FormView 和 RecordList 控件。

在下一個(gè)指南中,我們將研究解決數(shù)據(jù)庫,i.e,ADO.NET 的 技術(shù)。

但是,數(shù)據(jù)綁定包括以下對(duì)象:

  • 存儲(chǔ)從數(shù)據(jù)庫檢索數(shù)據(jù)的數(shù)據(jù)集。
  • 數(shù)據(jù)提供者,它通過使用一個(gè)連接的命令從數(shù)據(jù)庫中檢索數(shù)據(jù)。
  • 發(fā)出存儲(chǔ)在 command 對(duì)象中的選擇語句的數(shù)據(jù)適配器;它也能通過發(fā)出 Insert,Delete,和 Updata 語句來更新數(shù)據(jù)庫中的數(shù)據(jù)。

data bonding 對(duì)象間的關(guān)系:

http://wiki.jikexueyuan.com/project/asp-net/images/declarative_data_binding.jpg" alt="3" />

例子

讓我們采取以下的步驟:

步驟(1):創(chuàng)建一個(gè)新的網(wǎng)頁。通過右擊在 Solution Explorer 上的 solution 名字和從 'Add Item' 對(duì)話框中選擇項(xiàng)目 'Class' 來添加一個(gè)名為 booklist 的類。將它命名為 booklist.cs。

using System;
using System.Data;
using System.Configuration;
using System.Linq;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

namespace databinding
{
   public class booklist
   {
      protected String bookname;
      protected String authorname;
      public booklist(String bname, String aname)
      {
         this.bookname = bname;
         this.authorname = aname;

      }

      public String Book
      {
         get
         {
            return this.bookname;
         }
         set
         {
            this.bookname = value;
         }
      }

      public String Author
      {
         get
         {
            return this.authorname;
         }
         set
         {
            this.authorname = value;
         }
      }
   }
}

步驟(2):在頁面上添加四個(gè)列表控件,一個(gè) list box 控件,一個(gè) radio button 控件,一個(gè) check box 控件和一個(gè) drop down list 和四個(gè)與這些列表控件一起的四個(gè)表單。在設(shè)計(jì)視圖中頁面應(yīng)該看起來像這樣:

http://wiki.jikexueyuan.com/project/asp-net/images/list_box_control.jpg" alt="4" />

源文件應(yīng)該看起來像下面這樣:

<form id="form1" runat="server">
   <div>

      <table style="width: 559px">
         <tr>
            <td style="width: 228px; height: 157px;">
               <asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" 
                  OnSelectedIndexChanged="ListBox1_SelectedIndexChanged">
               </asp:ListBox>
            </td>

            <td style="height: 157px">
               <asp:DropDownList ID="DropDownList1" runat="server" 
                  AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
               </asp:DropDownList>
            </td>             
         </tr>

         <tr>
            <td style="width: 228px; height: 40px;">
               <asp:Label ID="lbllistbox" runat="server"></asp:Label>
            </td>

            <td style="height: 40px">
               <asp:Label ID="lbldrpdown" runat="server">
               </asp:Label>
            </td>
         </tr>

         <tr>
            <td style="width: 228px; height: 21px">
            </td>

            <td style="height: 21px">
            </td>              
         </tr>

         <tr>
            <td style="width: 228px; height: 21px">
               <asp:RadioButtonList ID="RadioButtonList1" runat="server"
                  AutoPostBack="True"  OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
               </asp:RadioButtonList>
            </td>

            <td style="height: 21px">
               <asp:CheckBoxList ID="CheckBoxList1" runat="server" 
                  AutoPostBack="True" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged">
               </asp:CheckBoxList>
            </td>                
         </tr>

         <tr>
            <td style="width: 228px; height: 21px">
               <asp:Label ID="lblrdlist" runat="server">
               </asp:Label>
            </td>

            <td style="height: 21px">
               <asp:Label ID="lblchklist" runat="server">
               </asp:Label>
            </td>           
         </tr>
      </table>      

   </div>
</form>

步驟(3):最后,在應(yīng)用程序的例行程序后寫下面的代碼:

public partial class _Default : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
      IList bklist = createbooklist();

      if (!this.IsPostBack)
      {
         this.ListBox1.DataSource = bklist;
         this.ListBox1.DataTextField = "Book";
         this.ListBox1.DataValueField = "Author";

         this.DropDownList1.DataSource = bklist;
         this.DropDownList1.DataTextField = "Book";
         this.DropDownList1.DataValueField = "Author";

         this.RadioButtonList1.DataSource = bklist;
         this.RadioButtonList1.DataTextField = "Book";
         this.RadioButtonList1.DataValueField = "Author";

         this.CheckBoxList1.DataSource = bklist;
         this.CheckBoxList1.DataTextField = "Book";
         this.CheckBoxList1.DataValueField = "Author";

         this.DataBind();
      }
   }

   protected IList createbooklist()
   {
      ArrayList allbooks = new ArrayList();
      booklist bl;

      bl = new booklist("UNIX CONCEPTS", "SUMITABHA DAS");
      allbooks.Add(bl);

      bl = new booklist("PROGRAMMING IN C", "RICHI KERNIGHAN");
      allbooks.Add(bl);

      bl = new booklist("DATA STRUCTURE", "TANENBAUM");
      allbooks.Add(bl);

      bl = new booklist("NETWORKING CONCEPTS", "FOROUZAN");
      allbooks.Add(bl);

      bl = new booklist("PROGRAMMING IN C++", "B. STROUSTROUP");
      allbooks.Add(bl);

      bl = new booklist("ADVANCED JAVA", "SUMITABHA DAS");
      allbooks.Add(bl);

      return allbooks;
   }

   protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
   {
      this.lbllistbox.Text = this.ListBox1.SelectedValue;
   }

   protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
   {
      this.lbldrpdown.Text = this.DropDownList1.SelectedValue;
   }

   protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
   {
      this.lblrdlist.Text = this.RadioButtonList1.SelectedValue;
   }

   protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
   {
      this.lblchklist.Text = this.CheckBoxList1.SelectedValue;
   }
}

觀察以下:

  • booklist 類有兩個(gè)屬性:bookname 和 authorname。
  • createbooklist 方法是一個(gè)用戶定義的可以創(chuàng)建名為 allboods 的 booklist 類的數(shù)組的方法。
  • Page_Load 事件句柄確保了 books 的列表被創(chuàng)建。該列表是 IList 型的,它實(shí)現(xiàn)了 IEnumerable 接口并能和列表控件綁定。Page load 時(shí)間句柄用控件綁定了 IList 對(duì)象'bklist'。bookname 屬性被展示并且 authorname 屬性被視為這個(gè)值?! ?/li>
  • 當(dāng)頁面運(yùn)行時(shí),如果用戶選擇了一本書,則它的名字被選擇并且通過 list 控件被顯示出來,而相應(yīng)的標(biāo)簽顯示作者的名字,它是 list 控件所選擇的相應(yīng)的值。

http://wiki.jikexueyuan.com/project/asp-net/images/data_binding_result.jpg" alt="5" />

上一篇:HTML 服務(wù)器下一篇:調(diào)試