數據控件與業務實體之間的綁定小結
Author:Denny
Date:12/14/2007
數據綁定Eval方法VS數據綁定Bind方法
<%# Eval(“ProductID”) %>
<%# Bind(“ProductID”)%>
Eval:單向綁定,數據只讀
Bind:雙向綁定,可讀可執行Insert/Update/…..操作
詳細介紹:在ASP.Net的數據綁定的控件中,如GridView, DetailsView, 以及FormVIEw這些控件能夠自動對數據源進行Update, Delete 和 Insert操作。
if you have defined SQL Select, Insert, Delete, and Update statements for your data source control, using Bind in a GridVIEw, DetailsVIEw, or FormVIEw control template enables the control to extract values from child controls in the template and pass them to the data source control. The data source control in turn performs the appropriate command for the database.
由於以上原因,所以Bind方法通常用在數據控件中的EditItemTemplate或InsertItemTemplate模板中。
Demo:
<EditItemTemplate>
<table>
<tr>
<td align=right>
<b>Employee ID:</b>
</td>
<td>
<%# Eval("EmployeeID") %>
</td>
</tr>
<tr>
<td align=right>
<b>First Name:</b>
</td>
<td>
<ASP:TextBox ID="EditFirstNameTextBox" RunAt="Server"
Text=''<%# Bind("FirstName") %>'' />
</td>
&nb
$False$
sp; </tr>
<tr>
<td align=right>
<b>Last Name:</b>
</td>
<td>
<ASP:TextBox ID="EditLastNameTextBox" RunAt="Server"
Text=''<%# Bind("LastName") %>'' />
</td>
</tr>
<tr>
<td colspan="2">
<ASP:LinkButton ID="UpdateButton" RunAt="server"
Text="Update" CommandName="Update" />
<ASP:LinkButton ID="CancelUpdateButton" RunAt="server"
Text="Cancel" CommandName="Cancel" />
</td>
</tr>
</table>
</EditItemTemplate>
當點擊某行的Update按鈕時,被Bind方法綁定的TextBox值就會被傳入到數據源控件中進行更新操作。
與簡單的業務實體屬性綁定
簡單業務實體:
Class CArtwork
{
//公共屬性
Public int ID {…}
Public string Title {….}
Public string Owner {….}
Public DateTime DateCompleted {…}
Public bool IsRecommend {…}