在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ C#/ ASP.NET數(shù)據(jù)綁定
ASP.NET調(diào)試
ASP.NET Web Services
ASP.NET緩存
ASP.NET多線程
ASP.NET面板控件
ASP.NET數(shù)據(jù)綁定
ASP.NET數(shù)據(jù)源
ASP.NET個性化
ASP.Net教程
ASP.NET Ajax控件
ASP.NET生命周期
ASP.NET HTML服務器
ASP.NET簡介
ASP.NET驗證器
ASP.NET多視圖
ASP.NET網(wǎng)站配置
ASP.NET錯誤管理
ASP.NET自定義控件
ASP.NET LINQ
ASP.NET AdRotator控件
ASP.NET客戶端
ASP.NET文件上傳
ASP.NET服務器控件
ASP.NET開發(fā)環(huán)境配置
ASP.NET管理狀態(tài)
ASP.NET服務端
ASP.NET數(shù)據(jù)庫訪問(Access)
ASP.NET基本控件
ASP.NET安全
ASP.NET指令
ASP.NET事件處理
ASP.NET第一個程序
ASP.NET日歷控件

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

每個ASP.NET Web表單控件都從其父級控件類繼承DataBind方法,從而使其具有將數(shù)據(jù)綁定到其至少一個屬性。 這被稱為簡單數(shù)據(jù)綁定或內(nèi)聯(lián)數(shù)據(jù)綁定。

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

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

數(shù)據(jù)源控件幫助數(shù)據(jù)綁定控件實現(xiàn)諸如排序,分頁和編輯數(shù)據(jù)收集等功能。

BaseDataBoundControl是一個抽象類,由另外兩個抽象類繼承:

  • DataBoundControl
  • HierarchicalDataBoundControl

抽象類DataBoundControl再次被兩個抽象類繼承:

  • ListControl
  • CompositeDataBoundControl

能夠進行簡單數(shù)據(jù)綁定的控件是從ListControl抽象類派生的,這些控件是:

  • BulletedList
  • CheckBoxList
  • DropDownList
  • ListBox
  • RadioButtonList

能夠聲明性數(shù)據(jù)綁定(更復雜的數(shù)據(jù)綁定)的控件是從CompositeDataBoundControl抽象類派生的。這些控件是:

  • DetailsView
  • FormView
  • GridView
  • RecordList

簡單數(shù)據(jù)綁定

簡單數(shù)據(jù)綁定涉及只讀選擇列表。這些控件可以綁定到數(shù)組列表或數(shù)據(jù)庫表的字段。 選擇列表從數(shù)據(jù)庫或數(shù)據(jù)源獲取兩個值; 一個值顯示在列表中,另一個值被視為列相對應的值。

下面通過一個小例子來理解這個概念。創(chuàng)建一個帶有項目符號列表和一個SqlDataSource控件的網(wǎng)站。配置數(shù)據(jù)源控件來從數(shù)據(jù)庫中檢索兩個值(使用一個Access數(shù)據(jù)庫的一個學生表:student)。打開Visual Studio ,創(chuàng)建一個名稱為:DataBinding 的網(wǎng)站項目,參考下圖 -

再創(chuàng)建一個Web窗體頁面 - Default.aspx ,如下代碼 -

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>簡單數(shù)據(jù)綁定</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString %>" ProviderName="<%$ ConnectionStrings:DatabaseConnectionString.ProviderName %>" SelectCommand="SELECT [sname], [from] FROM [students]"></asp:SqlDataSource>
            <br />
            <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
                <AlternatingItemTemplate>
                    <li style="">sname:
                        <asp:Label ID="snameLabel" runat="server" Text='<%# Eval("sname") %>' />
                        <br />
                        from:
                        <asp:Label ID="fromLabel" runat="server" Text='<%# Eval("from") %>' />
                        <br />
                    </li>
                </AlternatingItemTemplate>
                <EditItemTemplate>
                    <li style="">sname:
                        <asp:TextBox ID="snameTextBox" runat="server" Text='<%# Bind("sname") %>' />
                        <br />
                        from:
                        <asp:TextBox ID="fromTextBox" runat="server" Text='<%# Bind("from") %>' />
                        <br />
                        <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="更新" />
                        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="取消" />
                    </li>
                </EditItemTemplate>
                <EmptyDataTemplate>
                    未返回數(shù)據(jù)。
                </EmptyDataTemplate>
                <InsertItemTemplate>
                    <li style="">sname:
                        <asp:TextBox ID="snameTextBox" runat="server" Text='<%# Bind("sname") %>' />
                        <br />from:
                        <asp:TextBox ID="fromTextBox" runat="server" Text='<%# Bind("from") %>' />
                        <br />
                        <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="插入" />
                        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="清除" />
                    </li>
                </InsertItemTemplate>
                <ItemSeparatorTemplate>
<br />
                </ItemSeparatorTemplate>
                <ItemTemplate>
                    <li style="">sname:
                        <asp:Label ID="snameLabel" runat="server" Text='<%# Eval("sname") %>' />
                        <br />
                        from:
                        <asp:Label ID="fromLabel" runat="server" Text='<%# Eval("from") %>' />
                        <br />
                    </li>
                </ItemTemplate>
                <LayoutTemplate>
                    <ul id="itemPlaceholderContainer" runat="server" style="">
                        <li runat="server" id="itemPlaceholder" />
                    </ul>
                    <div style="">
                    </div>
                </LayoutTemplate>
                <SelectedItemTemplate>
                    <li style="">sname:
                        <asp:Label ID="snameLabel" runat="server" Text='<%# Eval("sname") %>' />
                        <br />
                        from:
                        <asp:Label ID="fromLabel" runat="server" Text='<%# Eval("from") %>' />
                        <br />
                    </li>
                </SelectedItemTemplate>
            </asp:ListView>
        </div>
    </form>
</body>
</html>

假設Access數(shù)據(jù)庫中的students表有以下數(shù)據(jù) -

執(zhí)行應用程序時,請檢查整個snamefrom列是否綁定到項目符號列表并顯示。

聲明性數(shù)據(jù)綁定

在前面的教程文章中,我們已經(jīng)在GridView控件中使用了聲明式數(shù)據(jù)綁定。能夠以表格方式顯示和操作數(shù)據(jù)的其他復合數(shù)據(jù)綁定控件是DetailsView,FormViewRecordList控件。

在下一個教程中,我們將學習處理數(shù)據(jù)庫的技術(shù),即 ADO.NET ,有關(guān)ADO.NET 的詳細講解,請參考單獨的章節(jié):http://www.yiibai.com/ado.net 。

但是,數(shù)據(jù)綁定涉及以下對象:

  • 從數(shù)據(jù)庫檢索存儲數(shù)據(jù)的數(shù)據(jù)集。
  • 數(shù)據(jù)提供者,通過使用命令連接數(shù)據(jù)庫,并從數(shù)據(jù)庫中檢索數(shù)據(jù)。
  • 發(fā)出存儲在Command對象中的select語句的數(shù)據(jù)適配器; 它也能夠通過發(fā)出插入,刪除和更新語句來更新數(shù)據(jù)庫中的數(shù)據(jù)。

數(shù)據(jù)綁定對象之間的關(guān)系,請參考下圖:

示例

本示例基于上面示例中創(chuàng)建的DataBinding 的網(wǎng)站項目,參考以下實踐步驟:

第1步: 右鍵單擊【解決方案資源管理器】中的解決方案名稱,然后從“添加項目”對話框中選擇“類”項目,添加一個名為Book的類。將保存文件命名為Book.cs。參考以下實現(xiàn)代碼 -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Book 的摘要說明
/// </summary>
using System.Data;
using System.Configuration;
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;


    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步: 在頁面上添加四個列表控件,一個列表框控件,一個單選按鈕列表,一個復選框列表,一個下拉列表和四個標簽以及這些列表控件。設計視圖中的頁面應該如下所示:

在這個項目中添加一個窗體:Default2.aspx ,其代碼如下所示 -

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>聲明性數(shù)據(jù)綁定</title>
</head>
<body>
    <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>
</body>
</html>

第3步: 最后,將下面的代碼寫在文件:Default2.aspx.cs ,如下所示 -

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : 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類有兩個屬性:booknameauthorname
  • createbooklist方法是用戶定義的方法,它創(chuàng)建一個名為allbooks包含booklist對象數(shù)組。
  • Page_Load事件處理程序確保創(chuàng)建booklist。該列表是IList類型,它實現(xiàn)了IEnumerable接口,并且能夠綁定到List控件。頁面加載事件處理程序?qū)?code>IList對象“bklist”List控件的綁定。bookname屬性將被顯示,并且authorname屬性被視為值。
  • 當頁面運行時,如果用戶選擇了一本書,則其名稱被選擇并由List控件顯示,而相應的標簽顯示作者姓名,該名稱是List控件的所選索引的對應值。

運行上面項目,得到以下結(jié)果 -

選擇上面輸出結(jié)果的選項,得到以類似下面的結(jié)果 -


上一篇:ASP.NET服務端下一篇:ASP.Net教程