TemplateField.InsertItemTemplate 属性
定义
获取或设置模板,该模板用于显示 TemplateField 对象中处于插入模式中的项。Gets or sets the template for displaying an item in insert mode in a TemplateField object.
public:
virtual property System::Web::UI::ITemplate ^ InsertItemTemplate { System::Web::UI::ITemplate ^ get(); void set(System::Web::UI::ITemplate ^ value); };
[System.ComponentModel.Browsable(false)]
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
[System.Web.UI.TemplateContainer(typeof(System.Web.UI.IDataItemContainer), System.ComponentModel.BindingDirection.TwoWay)]
public virtual System.Web.UI.ITemplate InsertItemTemplate { get; set; }
[<System.ComponentModel.Browsable(false)>]
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
[<System.Web.UI.TemplateContainer(typeof(System.Web.UI.IDataItemContainer), System.ComponentModel.BindingDirection.TwoWay)>]
member this.InsertItemTemplate : System.Web.UI.ITemplate with get, set
Public Overridable Property InsertItemTemplate As ITemplate
属性值
一个实现了 ITemplate 的对象,该对象包含用于显示 TemplateField 中处于插入模式的项的模板。A ITemplate-implemented object that contains the template for displaying an item in insert mode in a TemplateField. 默认值为 null,指示未设置此属性。The default is null, which indicates that this property is not set.
- 属性
示例
下面的代码示例演示如何使用 InsertItemTemplate 属性为控件中的字段行中的 "插入" 模式下的项创建自定义模板 TemplateField DetailsView 。The following code example demonstrates how to use the InsertItemTemplate property to create a custom template for an item in insert mode in a TemplateField field row in a DetailsView control. 该模板显示一个 DropDownList 控件,该控件允许用户从预定义列表中选择值。The template displays a DropDownList control that allows the user to select a value from a predefined list.
<%@ 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 StoresGridView_SelectedIndexChanged(Object sender, EventArgs e)
{
// Set the DataItemIndex property of the DetailsView control to display
// the record selected from the GridView control.
// StoresDetailView.DataItemIndex = StoresGridView.SelectedIndex;
}
void StoresDetailView_ItemInserting(Object sender, DetailsViewInsertEventArgs e)
{
// Get the state value from the DropDownList control in the
// DetailsView control.
String state = GetState();
// Add the state to the dictionary of values to
// insert into the database.
e.Values["state"] = state;
}
void StoresDetailView_ItemInserted (Object sender, DetailsViewInsertedEventArgs e)
{
// Refresh the GridView control after a new record is inserted.
StoresGridView.DataBind ();
}
String GetState()
{
String state;
// Get the DropDownList control that contains the state value
// in the DetailsView control.
DropDownList list = (DropDownList)StoresDetailView.Rows[4].FindControl("StateList");
if (list != null)
{
// Get the selected value of the DropDownList control.
state = list.SelectedItem.Text;
}
else
{
// Set the state to an empty string ("").
state = "";
}
return state;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>TemplateField InsertItemTemplate Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>TemplateField InsertItemTemplate Example</h3>
<table cellspacing="10">
<tr>
<td>
<asp:gridview id="StoresGridView"
datasourceid="StoresSqlDataSource"
autogeneratecolumns="false"
autogenerateselectbutton="true"
datakeynames="stor_id"
onselectedindexchanged="StoresGridView_SelectedIndexChanged"
runat="server">
<headerstyle backcolor="Blue"
forecolor="White"/>
<columns>
<asp:boundfield datafield="stor_name"
headertext="Store Name"/>
<asp:boundfield datafield="stor_address"
headertext="Address"/>
<asp:boundfield datafield="city"
headertext="City"/>
<asp:boundfield datafield="state"
headertext="State"/>
<asp:boundfield datafield="zip"
headertext="ZIP Code"/>
</columns>
</asp:gridview>
</td>
<td valign="top">
<asp:detailsview id="StoresDetailView"
datasourceid="StoresDetailsSqlDataSource"
autogenerateinsertbutton="true"
autogeneraterows="false"
datakeynames="stor_id"
gridlines="Both"
oniteminserting="StoresDetailView_ItemInserting"
oniteminserted="StoresDetailView_ItemInserted"
runat="server">
<headerStyle backcolor="Navy"
forecolor="White"/>
<Fields>
<asp:boundfield datafield="stor_id"
headertext="Store ID"/>
<asp:boundfield datafield="stor_name"
headertext="Store Name"/>
<asp:boundfield datafield="stor_address"
headertext="Address"/>
<asp:boundfield datafield="city"
headertext="City"/>
<asp:templatefield headertext="State">
<itemtemplate>
<%#Eval("state")%>
</itemtemplate>
<insertitemtemplate>
<asp:dropdownlist id="StateList"
datasourceid="StateSqlDataSource"
datatextfield="state"
runat="server"/>
</insertitemtemplate>
</asp:templatefield>
<asp:boundfield datafield="zip"
headertext="ZIP Code"/>
</Fields>
</asp:detailsview>
</td>
</tr>
</table>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Pubs sample database. -->
<asp:sqldatasource id="StoresSqlDataSource"
selectcommand="SELECT [stor_id], [stor_name], [stor_address], [city], [state], [zip] FROM [stores]"
connectionstring="server=localhost;database=pubs;integrated security=SSPI"
runat="server">
</asp:sqldatasource>
<asp:sqldatasource id="StoresDetailsSqlDataSource"
selectcommand="SELECT [stor_id], [stor_name], [stor_address], [city], [state], [zip] FROM [stores]"
insertcommand="INSERT INTO stores([stor_id], [stor_name], [stor_address], [city], [state], [zip]) VALUES (@stor_id, @stor_name, @stor_address, @city, @state, @zip)"
connectionstring="server=localhost;database=pubs;integrated security=SSPI"
runat="server">
</asp:sqldatasource>
<!-- For this example, the states are retrieved from the -->
<!-- state field. For your application, you should use a -->
<!-- more complete source for the state values. -->
<asp:sqldatasource id="StateSqlDataSource"
selectcommand="SELECT Distinct [state] FROM [stores]"
connectionstring="server=localhost;database=pubs;integrated security=SSPI"
runat="server">
</asp:sqldatasource>
</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 StoresGridView_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
' Set the DataItemIndex property of the DetailsView control to display
' the record selected from the GridView control.
' StoresDetailView.DataItem = StoresGridView.SelectedIndex
End Sub
Sub StoresDetailView_ItemInserting(ByVal sender As Object, ByVal e As DetailsViewInsertEventArgs)
' Get the state value from the DropDownList control in the
' DetailsView control.
Dim state As String = GetState()
' Add the state to the dictionary of values to
' insert into the database.
e.Values("state") = state
End Sub
Sub StoresDetailView_ItemInserted(ByVal sender As Object, ByVal e As DetailsViewInsertedEventArgs)
' Refresh the GridView control after a new record is inserted.
StoresGridView.DataBind()
End Sub
Function GetState() As String
Dim state As String
' Get the DropDownList control that contains the state value
' in the DetailsView control.
Dim list As DropDownList = CType(StoresDetailView.Rows(4).FindControl("StateList"), DropDownList)
If Not list Is Nothing Then
' Get the selected value of the DropDownList control.
state = list.SelectedItem.Text
Else
' Set the state to an empty string ("").
state = ""
End If
Return state
End Function
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>TemplateField InsertItemTemplate Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>TemplateField InsertItemTemplate Example</h3>
<table cellspacing="10">
<tr>
<td>
<asp:gridview id="StoresGridView"
datasourceid="StoresSqlDataSource"
autogeneratecolumns="false"
autogenerateselectbutton="true"
datakeynames="stor_id"
onselectedindexchanged="StoresGridView_SelectedIndexChanged"
runat="server">
<headerstyle backcolor="Blue"
forecolor="White"/>
<columns>
<asp:boundfield datafield="stor_name"
headertext="Store Name"/>
<asp:boundfield datafield="stor_address"
headertext="Address"/>
<asp:boundfield datafield="city"
headertext="City"/>
<asp:boundfield datafield="state"
headertext="State"/>
<asp:boundfield datafield="zip"
headertext="ZIP Code"/>
</columns>
</asp:gridview>
</td>
<td valign="top">
<asp:detailsview id="StoresDetailView"
datasourceid="StoresDetailsSqlDataSource"
autogenerateinsertbutton="true"
autogeneraterows="false"
datakeynames="stor_id"
gridlines="Both"
oniteminserting="StoresDetailView_ItemInserting"
oniteminserted="StoresDetailView_ItemInserted"
runat="server">
<headerstyle backcolor="Navy"
forecolor="White"/>
<fields>
<asp:boundfield datafield="stor_id"
headertext="Store ID"/>
<asp:boundfield datafield="stor_name"
headertext="Store Name"/>
<asp:boundfield datafield="stor_address"
headertext="Address"/>
<asp:boundfield datafield="city"
headertext="City"/>
<asp:templatefield headertext="State">
<itemtemplate>
<%#Eval("state")%>
</itemtemplate>
<insertitemtemplate>
<asp:dropdownlist id="StateList"
datasourceid="StateSqlDataSource"
datatextfield="state"
runat="server"/>
</insertitemtemplate>
</asp:templatefield>
<asp:boundfield datafield="zip"
headertext="ZIP Code"/>
</fields>
</asp:detailsview>
</td>
</tr>
</table>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Pubs sample database. -->
<asp:sqldatasource id="StoresSqlDataSource"
selectcommand="SELECT [stor_id], [stor_name], [stor_address], [city], [state], [zip] FROM [stores]"
connectionstring="server=localhost;database=pubs;integrated security=SSPI"
runat="server">
</asp:sqldatasource>
<asp:sqldatasource id="StoresDetailsSqlDataSource"
selectcommand="SELECT [stor_id], [stor_name], [stor_address], [city], [state], [zip] FROM [stores]"
insertcommand="INSERT INTO stores([stor_id], [stor_name], [stor_address], [city], [state], [zip]) VALUES (@stor_id, @stor_name, @stor_address, @city, @state, @zip)"
connectionstring="server=localhost;database=pubs;integrated security=SSPI"
runat="server">
</asp:sqldatasource>
<!-- For this example, the states are retrieved from the -->
<!-- state field. For your application, you should use a -->
<!-- more complete source for the state values. -->
<asp:sqldatasource id="StateSqlDataSource"
selectcommand="SELECT Distinct [state] FROM [stores]"
connectionstring="server=localhost;database=pubs;integrated security=SSPI"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
注解
使用 " InsertItemTemplate 属性" 指定为对象中的 "插入" 模式下的项显示的自定义内容 TemplateField 。Use the InsertItemTemplate property to specify the custom content displayed for an item in insert mode in a TemplateField object. 通过创建一个模板来定义内容,该模板指定如何呈现处于插入模式的项。Define the content by creating a template that specifies how the item in insert mode is rendered.
若要指定模板,请先在 <InsertItemTemplate> 元素的开始标记和结束标记之间放置开始标记和结束标记 <TemplateField> 。To specify a template, first place opening and closing <InsertItemTemplate> tags between the opening and closing tags of the <TemplateField> element. 接下来,在开始标记和结束标记之间添加自定义内容 <InsertItemTemplate> 。Next, add the custom content between the opening and closing <InsertItemTemplate> tags. 内容可以与纯文本或更复杂 (嵌入模板中的其他控件一样简单,例如) 。The content can be as simple as plain text or more complex (embedding other controls in the template, for example).
若要以编程方式访问在模板中定义的控件,首先要确定 TableCell 数据绑定控件中包含控件的对象。To programmatically access a control defined in a template, first determine which TableCell object in the data-bound control contains the control. 接下来,使用 Controls 对象的集合 TableCell 访问该控件。Next, use the Controls collection of the TableCell object to access the control. FindControl TableCell 如果控件具有指定的属性,则还可以使用对象的方法来查找控件 ID 。You can also use the FindControl method of the TableCell object to find the control, if the control has an ID property specified.
备注
并非所有数据绑定控件都支持此模板仅允许插入记录的数据绑定控件(如控件)支持此模板 DetailsView 。Not all data-bound controls support this template This template is supported only by data-bound controls that allow you to insert a record, such as the DetailsView control.