IBindableTemplate 接口
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为 ASP.NET 数据绑定控件(如 DetailsView 和 FormView)提供一种可以自动绑定到模板化内容节中的 ASP.NET 数据源控件的方式。
public interface class IBindableTemplate : System::Web::UI::ITemplate
public interface IBindableTemplate : System.Web.UI.ITemplate
type IBindableTemplate = interface
interface ITemplate
Public Interface IBindableTemplate
Implements ITemplate
- 派生
- 实现
示例
下面的代码示例演示了 FormView 控件如何以声明方式定义模板化内容、绑定到控件提供 SqlDataSource 的数据以及显示和编辑现有记录。 ASP.NET 分析器分析模板化内容并在运行时创建一个IBindableTemplate对象,该对象能够将控件中的SqlDataSource值绑定到模板中定义的数据绑定区域,方法是单向 ASP.NET 数据绑定语法 (<%# Eval("fieldname") %>
) 和双向数据绑定语法 (<%# Bind("fieldname") %>
) 。
重要
此控件具有接受用户输入的文本框,这是潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述。
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void EmployeeFormView_ItemUpdating(Object sender, FormViewUpdateEventArgs e)
{
// Validate the field values entered by the user. This
// example determines whether the user left any fields
// empty. Use the NewValues property to access the new
// values entered by the user.
ArrayList emptyFieldList = ValidateFields(e.NewValues);
if (emptyFieldList.Count > 0)
{
// The user left some fields empty. Display an error message.
// Use the Keys property to retrieve the key field value.
String keyValue = e.Keys["EmployeeID"].ToString();
MessageLabel.Text = "You must enter a value for each field of record " +
keyValue + ".<br/>The following fields are missing:<br/><br/>";
// Display the missing fields.
foreach (String value in emptyFieldList)
{
// Use the OldValues property to access the original value
// of a field.
MessageLabel.Text += value + " - Original Value = " +
e.OldValues[value].ToString() + "<br />";
}
// Cancel the update operation.
e.Cancel = true;
}
else
{
// The field values passed validation. Clear the
// error message label.
MessageLabel.Text = "";
}
}
ArrayList ValidateFields(IOrderedDictionary list)
{
// Create an ArrayList object to store the
// names of any empty fields.
ArrayList emptyFieldList = new ArrayList();
// Iterate though the field values entered by
// the user and check for an empty field. Empty
// fields contain a null value.
foreach (DictionaryEntry entry in list)
{
if (entry.Value == String.Empty)
{
// Add the field name to the ArrayList object.
emptyFieldList.Add(entry.Key.ToString());
}
}
return emptyFieldList;
}
void EmployeeFormView_ModeChanging(Object sender, FormViewModeEventArgs e)
{
if (e.CancelingEdit)
{
// The user canceled the update operation.
// Clear the error message label.
MessageLabel.Text = "";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>FormView Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>FormView Example</h3>
<asp:formview id="EmployeeFormView"
datasourceid="EmployeeSource"
allowpaging="true"
datakeynames="EmployeeID"
headertext="Employee Record"
emptydatatext="No employees found."
onitemupdating="EmployeeFormView_ItemUpdating"
onmodechanging="EmployeeFormView_ModeChanging"
runat="server">
<headerstyle backcolor="CornFlowerBlue"
forecolor="White"
font-size="14"
horizontalalign="Center"
wrap="false"/>
<rowstyle backcolor="LightBlue"
wrap="false"/>
<pagerstyle backcolor="CornFlowerBlue"/>
<itemtemplate>
<table>
<tr>
<td rowspan="6">
<asp:image id="EmployeeImage"
imageurl='<%# Eval("PhotoPath") %>'
alternatetext='<%# Eval("LastName") %>'
runat="server"/>
</td>
<td colspan="2">
</td>
</tr>
<tr>
<td>
<b>Name:</b>
</td>
<td>
<%# Eval("FirstName") %> <%# Eval("LastName") %>
</td>
</tr>
<tr>
<td>
<b>Title:</b>
</td>
<td>
<%# Eval("Title") %>
</td>
</tr>
<tr>
<td>
<b>Hire Date:</b>
</td>
<td>
<%# Eval("HireDate","{0:d}") %>
</td>
</tr>
<tr style="height:150; vertical-align:top">
<td>
<b>Address:</b>
</td>
<td>
<%# Eval("Address") %><br/>
<%# Eval("City") %> <%# Eval("Region") %>
<%# Eval("PostalCode") %><br/>
<%# Eval("Country") %>
</td>
</tr>
<tr>
<td colspan="2">
<asp:linkbutton id="Edit"
text="Edit"
commandname="Edit"
runat="server"/>
</td>
</tr>
</table>
</itemtemplate>
<edititemtemplate>
<table>
<tr>
<td rowspan="6">
<asp:image id="EmployeeEditImage"
imageurl='<%# Eval("PhotoPath") %>'
alternatetext='<%# Eval("LastName") %>'
runat="server"/>
</td>
<td colspan="2">
</td>
</tr>
<tr>
<td>
<b>Name:</b>
</td>
<td>
<asp:textbox id="FirstNameUpdateTextBox"
text='<%# Bind("FirstName") %>'
runat="server"/>
<asp:textbox id="LastNameUpdateTextBox"
text='<%# Bind("LastName") %>'
runat="server"/>
</td>
</tr>
<tr>
<td>
<b>Title:</b>
</td>
<td>
<asp:textbox id="TitleUpdateTextBox"
text='<%# Bind("Title") %>'
runat="server"/>
</td>
</tr>
<tr>
<td>
<b>Hire Date:</b>
</td>
<td>
<asp:textbox id="HireDateUpdateTextBox"
text='<%# Bind("HireDate", "{0:d}") %>'
runat="server"/>
</td>
</tr>
<tr style="height:150; vertical-align:top">
<td>
<b>Address:</b>
</td>
<td>
<asp:textbox id="AddressUpdateTextBox"
text='<%# Bind("Address") %>'
runat="server"/>
<br/>
<asp:textbox id="CityUpdateTextBox"
text='<%# Bind("City") %>'
runat="server"/>
<asp:textbox id="RegionUpdateTextBox"
text='<%# Bind("Region") %>'
width="40"
runat="server"/>
<asp:textbox id="PostalCodeUpdateTextBox"
text='<%# Bind("PostalCode") %>'
width="60"
runat="server"/>
<br/>
<asp:textbox id="CountryUpdateTextBox"
text='<%# Bind("Country") %>'
runat="server"/>
</td>
</tr>
<tr>
<td colspan="2">
<asp:linkbutton id="UpdateButton"
text="Update"
commandname="Update"
runat="server"/>
<asp:linkbutton id="CancelButton"
text="Cancel"
commandname="Cancel"
runat="server"/>
</td>
</tr>
</table>
</edititemtemplate>
<pagersettings position="Bottom"
mode="Numeric"/>
</asp:formview>
<br/><br/>
<asp:label id="MessageLabel"
forecolor="Red"
runat="server"/>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="EmployeeSource"
selectcommand="Select [EmployeeID], [LastName], [FirstName], [Title], [Address], [City], [Region], [PostalCode], [Country], [HireDate], [PhotoPath] From [Employees]"
updatecommand="Update [Employees] Set [LastName]=@LastName, [FirstName]=@FirstName, [Title]=@Title, [Address]=@Address, [City]=@City, [Region]=@Region, [PostalCode]=@PostalCode, [Country]=@Country Where [EmployeeID]=@EmployeeID"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>
</form>
</body>
</html>
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub EmployeeFormView_ItemUpdating(ByVal sender As Object, ByVal e As FormViewUpdateEventArgs) Handles EmployeeFormView.ItemUpdating
' Validate the field values entered by the user. This
' example determines whether the user left any fields
' empty. Use the NewValues property to access the new
' values entered by the user.
Dim emptyFieldList As ArrayList = ValidateFields(e.NewValues)
If emptyFieldList.Count > 0 Then
' The user left some fields empty. Display an error message.
' Use the Keys property to retrieve the key field value.
Dim keyValue As String = e.Keys("EmployeeID").ToString()
MessageLabel.Text = "You must enter a value for each field of record " & _
keyValue & ".<br/>The following fields are missing:<br/><br/>"
' Display the missing fields.
Dim value As String
For Each value In emptyFieldList
' Use the OldValues property to access the original value
' of a field.
MessageLabel.Text &= value & " - Original Value = " & _
e.OldValues(value).ToString() & "<br />"
Next
' Cancel the update operation.
e.Cancel = True
Else
' The field values passed validation. Clear the
' error message label.
MessageLabel.Text = ""
End If
End Sub
Function ValidateFields(ByVal list As IOrderedDictionary) As ArrayList
' Create an ArrayList object to store the
' names of any empty fields.
Dim emptyFieldList As New ArrayList()
' Iterate though the field values entered by
' the user and check for an empty field. Empty
' fields contain a null value.
Dim entry As DictionaryEntry
For Each entry In list
If entry.Value Is String.Empty Then
' Add the field name to the ArrayList object.
emptyFieldList.Add(entry.Key.ToString())
End If
Next
Return emptyFieldList
End Function
Sub EmployeeFormView_ModeChanging(ByVal sender As Object, ByVal e As FormViewModeEventArgs) Handles EmployeeFormView.ModeChanging
If e.CancelingEdit Then
' The user canceled the update operation.
' Clear the error message label.
MessageLabel.Text = ""
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>FormView Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>FormView Example</h3>
<asp:formview id="EmployeeFormView"
datasourceid="EmployeeSource"
allowpaging="true"
datakeynames="EmployeeID"
headertext="Employee Record"
emptydatatext="No employees found."
runat="server">
<headerstyle backcolor="CornFlowerBlue"
forecolor="White"
font-size="14"
horizontalalign="Center"
wrap="false"/>
<rowstyle backcolor="LightBlue"
wrap="false"/>
<pagerstyle backcolor="CornFlowerBlue"/>
<itemtemplate>
<table>
<tr>
<td rowspan="6">
<asp:image id="EmployeeImage"
imageurl='<%# Eval("PhotoPath") %>'
alternatetext='<%# Eval("LastName") %>'
runat="server"/>
</td>
<td colspan="2">
</td>
</tr>
<tr>
<td>
<b>Name:</b>
</td>
<td>
<%# Eval("FirstName") %> <%# Eval("LastName") %>
</td>
</tr>
<tr>
<td>
<b>Title:</b>
</td>
<td>
<%# Eval("Title") %>
</td>
</tr>
<tr>
<td>
<b>Hire Date:</b>
</td>
<td>
<%# Eval("HireDate","{0:d}") %>
</td>
</tr>
<tr style="height:150; vertical-align:top">
<td>
<b>Address:</b>
</td>
<td>
<%# Eval("Address") %><br/>
<%# Eval("City") %> <%# Eval("Region") %>
<%# Eval("PostalCode") %><br/>
<%# Eval("Country") %>
</td>
</tr>
<tr>
<td colspan="2">
<asp:linkbutton id="Edit"
text="Edit"
commandname="Edit"
runat="server"/>
</td>
</tr>
</table>
</itemtemplate>
<edititemtemplate>
<table>
<tr>
<td rowspan="6">
<asp:image id="EmployeeEditImage"
imageurl='<%# Eval("PhotoPath") %>'
alternatetext='<%# Eval("LastName") %>'
runat="server"/>
</td>
<td colspan="2">
</td>
</tr>
<tr>
<td>
<b>Name:</b>
</td>
<td>
<asp:textbox id="FirstNameUpdateTextBox"
text='<%# Bind("FirstName") %>'
runat="server"/>
<asp:textbox id="LastNameUpdateTextBox"
text='<%# Bind("LastName") %>'
runat="server"/>
</td>
</tr>
<tr>
<td>
<b>Title:</b>
</td>
<td>
<asp:textbox id="TitleUpdateTextBox"
text='<%# Bind("Title") %>'
runat="server"/>
</td>
</tr>
<tr>
<td>
<b>Hire Date:</b>
</td>
<td>
<asp:textbox id="HireDateUpdateTextBox"
text='<%# Bind("HireDate", "{0:d}") %>'
runat="server"/>
</td>
</tr>
<tr style="height:150; vertical-align:top">
<td>
<b>Address:</b>
</td>
<td>
<asp:textbox id="AddressUpdateTextBox"
text='<%# Bind("Address") %>'
runat="server"/>
<br/>
<asp:textbox id="CityUpdateTextBox"
text='<%# Bind("City") %>'
runat="server"/>
<asp:textbox id="RegionUpdateTextBox"
text='<%# Bind("Region") %>'
width="40"
runat="server"/>
<asp:textbox id="PostalCodeUpdateTextBox"
text='<%# Bind("PostalCode") %>'
width="60"
runat="server"/>
<br/>
<asp:textbox id="CountryUpdateTextBox"
text='<%# Bind("Country") %>'
runat="server"/>
</td>
</tr>
<tr>
<td colspan="2">
<asp:linkbutton id="UpdateButton"
text="Update"
commandname="Update"
runat="server"/>
<asp:linkbutton id="CancelButton"
text="Cancel"
commandname="Cancel"
runat="server"/>
</td>
</tr>
</table>
</edititemtemplate>
<pagersettings position="Bottom"
mode="Numeric"/>
</asp:formview>
<br/><br/>
<asp:label id="MessageLabel"
forecolor="Red"
runat="server"/>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="EmployeeSource"
selectcommand="Select [EmployeeID], [LastName], [FirstName], [Title], [Address], [City], [Region], [PostalCode], [Country], [HireDate], [PhotoPath] From [Employees]"
updatecommand="Update [Employees] Set [LastName]=@LastName, [FirstName]=@FirstName, [Title]=@Title, [Address]=@Address, [City]=@City, [Region]=@Region, [PostalCode]=@PostalCode, [Country]=@Country Where [EmployeeID]=@EmployeeID"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>
</form>
</body>
</html>
注解
该IBindableTemplate接口为 ASP.NET 数据绑定控件(例如DetailsView,GridView以及FormView)绑定到 ASP.NET 数据源控件提供的数据(例如ObjectDataSource,或SqlDataSource数据绑定控件包含模板化内容)提供的方法。
备注
页面开发人员不实现 IBindableTemplate 接口。 创建自定义数据绑定控件的开发人员可能会在实现和ExtractRowValues方法中PerformDataBinding操作IBindableTemplate对象,但不实现自己的IBindableTemplate对象。
数据绑定控件(例如 GridView 通常知道其子控件树),因此可以将值绑定到它们、从它们中提取值,并在数据绑定控件和数据源控件之间传递这些值(每当发生数据绑定时)。 但是,当页面开发人员为数据绑定控件定义模板化内容时,模板中的子控件对父数据绑定控件不可见:父控件可以呈现子内容 (,因为控件实际上) 呈现自己,但它无法提取这些子控件的值以传递给关联的数据源控件的更新, 插入或删除操作。 在数据绑定方案中,模板化内容对父数据绑定控件不透明。 通过语法 Bind
,数据绑定控件可以从实例内 IBindableTemplate 绑定的控件数据中提取值。
数据绑定可以是单向绑定或双向绑定。 (枚举定义这些绑定方向 BindingDirection 。) 单向数据绑定是指在出站方向执行的任何数据绑定,从数据源控件到数据绑定控件;例如,任何数据读取方案都涉及单向数据绑定。 对于单向数据绑定,可以在模板内容中使用单向数据绑定语法 (<%# Eval("fieldname") %>
) ,无需使用双向 ASP.NET 数据绑定语法。 双向数据绑定描述从数据绑定控件到数据源控件的入站方向的数据绑定。 使用数据绑定和数据源控件 ASP.NET 自动编辑、插入和删除方案是双向数据绑定方案。 这些方案使用双向数据绑定表达式 (<%# Bind("fieldname") %>
) 。 接口IBindableTemplate和 ASP.NET 基础结构支持 ASP.NET 数据源控件和模板内容之间的自动、声明性双向数据绑定。 有关 ASP.NET 数据绑定表达式和语法的详细信息,请参阅“绑定到数据库和数据绑定表达式概述”。
数据绑定控件的模板化内容通常以声明方式定义。 下表描述了最常用于将模板化数据绑定到数据绑定控件的过程。
数据绑定控件 | 过程 |
---|---|
DetailsView | 数据绑定控件使用DataSourceID 数据源控件的属性绑定到数据,模板化内容在或InsertItemTemplate属性中ItemTemplateEditItemTemplate定义。 |
GridView | 数据绑定控件使用DataSourceID 数据源控件的属性绑定到数据,模板化内容在或EditItemTemplate属性中ItemTemplate定义。 控件 GridView 不支持插入操作。 |
FormView | 数据绑定控件使用DataSourceID 属性绑定到数据,模板化内容在对象ItemTemplateInsertItemTemplate或EditItemTemplate属性中TemplateField定义。 |
DataList控件Repeater不支持自动双向数据绑定方案。
ASP.NET 在分析模板中绑定到 ASP.NET 数据源控件的模板化内容时隐式创建IBindableTemplate对象。 具体而言,ASP.NET 分析器在分析使用 ASP.NET 数据绑定语法的模板化内容时创建类的CompiledBindableTemplateBuilder实例,并包含支持数据绑定的 ASP.NET Web 服务器控件。 这些 ASP.NET 分片控件由BindableAttribute属性标记。
接口 IBindableTemplate 定义一个方法 ExtractValues。 此方法针对双向数据绑定进行定义,以便数据绑定控件可以从模板内容自动提取名称/值对,并在运行时将对传递给数据源控件。 若要使自动数据绑定成功,方法 ExtractValues 从模板化内容中提取的字段名称必须与关联的数据源控件中的参数名称匹配。 控件开发人员仅在自定义数据绑定控件的一些类似方法的实现ExtractRowValues中显式调用ExtractValues该方法。
方法
ExtractValues(Control) |
通过类来实现时,使用模板化内容中的双向 ASP.NET 数据绑定语法对绑定值的一组名称/值对进行检索。 |
InstantiateIn(Control) |
当由类实现时,定义子控件和模板所属的 Control 对象。 然后在内联模板中定义这些子控件。 (继承自 ITemplate) |