此文檔解決以下問題:
一、簡單數據綁定
1.屬性綁定
語法:<%# 屬性名稱 %>
注意:需要調用Page類的DataBind方法才能執行綁定操作
2.表達式綁定
3.集合綁定
4.方法綁定
1.屬性綁定
(1)新建一個網站,默認主頁為Default.aspx。在Default.aspx頁的后台代碼文件中定義兩個公共屬性,這兩個屬性作為數據綁定式的數據源。代碼如下:
public string DName { get { return "屬性綁定DName"; } } public string DPrice { get { return "屬性綁定DPrice"; } }
(2)設置完數據綁定中的數據源,現在即可將它與顯示控件之間建立綁定關系。將視圖切換到源視圖,代碼如下:
<div> 屬性綁定:<br /> <%# DName %><br /> <%# DPrice %><br /> </div>
(3)綁定完成后,只需要在頁面的Page_Load事件中調用Page類的DataBind方法實現在頁面加載時讀取數據,代碼如下:
protected void Page_Load(object sender, EventArgs e) { //基於屬性的數據綁定所涉及的屬性必須包含get訪問器 //因為在數據綁定過程中,數據顯示控件需要通過屬性的get訪問器從屬性中讀取數據 Page.DataBind(); //DataBind方法通常在Page_Load事件中調用 }
(4)運行結果
2.表達式綁定
(1)新建一個網站,默認主頁為Default.aspx。在Default.aspx頁中添加相應控件,並設置相關屬性。代碼如下:
<div> 表達式綁定:<br /> 數量:<asp:TextBox ID="TextBox1" runat="server" Text="0"></asp:TextBox><br /> 單價:<asp:TextBox ID="TextBox2" runat="server" Text="0"></asp:TextBox><br /> <asp:Button ID="btnOK" runat="server" Text="確定" /><br /> <asp:Label ID="Label1" runat="server" Text=""></asp:Label><br /> </div>
(2)將視圖切換到源視圖,將表達式綁定到Label控件的Text屬性上,代碼如下:
<asp:Label ID="Label1" runat="server" Text='<%# "總金額:" +Convert.ToString(Convert.ToDecimal(TextBox1.Text)*Convert.ToInt32(TextBox2.Text))%>'></asp:Label><br />
(3)綁定完成后,只需要在頁面的Page_Load事件中調用Page類的DataBind方法實現在頁面加載時讀取數據,代碼如下:
protected void Page_Load(object sender, EventArgs e) { //基於屬性的數據綁定所涉及的屬性必須包含get訪問器 //因為在數據綁定過程中,數據顯示控件需要通過屬性的get訪問器從屬性中讀取數據 Page.DataBind(); //DataBind方法通常在Page_Load事件中調用 }
(4)運行結果
3.集合綁定
有一些服務器控件是多記錄控件,如DropDownList控件,這類控件即可使用集合作為數據源對其進行綁定。通常情況下,集合數據源主要包括ArrayList、Hashtabel、DataView、DataReader等。
(1)新建一個網站,默認主頁為Default.aspx。在Default.aspx頁中添加一個DropDownList控件作為顯示控件,並在后台頁面中定義一個ArrayList數據源,然后將數據綁定在顯示控件上,最后調用DataBind方法執行數據綁定並顯示數據。代碼如下:
<div> 集合綁定:<br /> <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList> </div>
protected void Page_Load(object sender, EventArgs e) { //3.集合綁定 //使用ArrayList類,引入或者指明命名空間System.Collections ArrayList arraylist = new ArrayList(); arraylist.Add("程序設計"); arraylist.Add("數據庫"); arraylist.Add("網頁制作"); DropDownList1.DataSource = arraylist; DropDownList1.DataBind(); }
(2)運行結果
4.方法綁定
定義一個方法,其中可以定義表達式計算的幾種方式,在數據綁定表達式中通過傳遞不同的參數得到調用方法的結果。
(1)新建一個網站,默認主頁為Default.aspx。在Default.aspx頁添加相關控件並設置其屬性。代碼如下:
<div> 方法綁定:<br /> 第一個數:<asp:TextBox ID="txtNum1" runat="server" Text="0"></asp:TextBox><br /> 第二個數:<asp:TextBox ID="txtNum2" runat="server" Text="0"></asp:TextBox><br /> 運算符:<asp:DropDownList ID="DropDownList2" runat="server"> <asp:ListItem>+</asp:ListItem> <asp:ListItem>-</asp:ListItem> <asp:ListItem>*</asp:ListItem> <asp:ListItem>/</asp:ListItem> </asp:DropDownList><br /> <asp:Button ID="Button1" runat="server" Text="確定" /><br /> <asp:Label ID="Label2" runat="server" Text=""></asp:Label><br /> </div>
(2)在后台代碼中編寫方法,代碼如下
//4.方法綁定 public string operation(string VarOperator) { double num1 = Convert.ToDouble(txtNum1.Text); double num2 = Convert.ToDouble(txtNum2.Text); double result = 0; switch (VarOperator) { case "+": result = num1 + num2; break; case "-": result = num1 - num2; break; case "*": result = num1 * num2; break; case "/": result = num1 / num2; break; default: break; } return result.ToString(); }
(3)在源視圖中,將方法的返回值綁定到Label的Text屬性,代碼如下:
<asp:Label ID="Label2" runat="server" Text='<%# operation(DropDownList2.SelectedValue)%>'></asp:Label><br />
(4)調用DataBind方法
protected void Page_Load(object sender, EventArgs e) { //基於屬性的數據綁定所涉及的屬性必須包含get訪問器 //因為在數據綁定過程中,數據顯示控件需要通過屬性的get訪問器從屬性中讀取數據 Page.DataBind(); //DataBind方法通常在Page_Load事件中調用 }
(5)運行結果