ListView.InsertItemTemplate 屬性

定義

取得或設定 ListView 控制項中的插入項目的自訂內容。Gets or sets the custom content for an insert item in the ListView control.

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.WebControls.ListViewItem), 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.WebControls.ListViewItem), System.ComponentModel.BindingDirection.TwoWay)>]
member this.InsertItemTemplate : System.Web.UI.ITemplate with get, set
Public Overridable Property InsertItemTemplate As ITemplate

屬性值

ITemplate

物件,包含 ListView 控制項中的插入項目的自訂內容。An object that contains the custom content for the insert item in the ListView control. 預設值為 null,表示這個屬性尚未設定。The default is null, which indicates that this property is not set.

屬性

範例

下列範例顯示如何為控制項的插入專案定義自訂範本 ListViewThe following example shows how to define a custom template for the insert item of a ListView control. 這個程式碼範例是針對事件提供的較大範例的一部分 ItemInsertedThis code example is part of a larger example provided for the ItemInserted event.

重要

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。This example has a text box that accepts user input, which is a potential security threat. 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。For more information, see Script Exploits Overview.

<asp:ListView ID="ContactsListView" 
  DataSourceID="ContactsDataSource" 
  DataKeyNames="ContactID"
  OnItemInserted="ContactsListView_ItemInserted"  
  InsertItemPosition="LastItem"
  runat="server">
  <LayoutTemplate>
    <table cellpadding="2" border="1" runat="server" id="tblContacts" width="640px">
      <tr runat="server" id="itemPlaceholder" />
    </table>
    <asp:DataPager runat="server" ID="PeopleDataPager" PageSize="12">
      <Fields>
        <asp:NextPreviousPagerField 
          ShowFirstPageButton="true" ShowLastPageButton="true"
          FirstPageText="|&lt;&lt; " LastPageText=" &gt;&gt;|"
          NextPageText=" &gt; " PreviousPageText=" &lt; " />
      </Fields>
    </asp:DataPager>
  </LayoutTemplate>
  <ItemTemplate>
    <tr runat="server">
      <td valign="top">
        <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
        <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
      </td>
      <td>&nbsp;
        <asp:Label ID="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
      </td>
    </tr>
  </ItemTemplate>
  <InsertItemTemplate>
    <tr style="background-color:#D3D3D3">
      <td valign="top">
        <asp:Label runat="server" ID="FirstNameLabel" 
          AssociatedControlID="FirstNameTextBox" Text="First Name"/>
        <asp:TextBox ID="FirstNameTextBox" runat="server" 
          Text='<%#Bind("FirstName") %>' /><br />
        <asp:Label runat="server" ID="LastNameLabel" 
          AssociatedControlID="LastNameTextBox" Text="Last Name" />
        <asp:TextBox ID="LastNameTextBox" runat="server" 
          Text='<%#Bind("LastName") %>' /><br />
        <asp:Label runat="server" ID="EmailLabel" 
          AssociatedControlID="EmailTextBox" Text="Email" />
        <asp:TextBox ID="EmailTextBox" runat="server" 
          Text='<%#Bind("EmailAddress") %>' />
      </td>
      <td>
        <asp:LinkButton ID="InsertButton" runat="server" 
          CommandName="Insert" Text="Insert" />
      </td>
    </tr>
  </InsertItemTemplate>
</asp:ListView>
<asp:ListView ID="ContactsListView" 
  DataSourceID="ContactsDataSource" 
  DataKeyNames="ContactID"
  OnItemInserted="ContactsListView_ItemInserted"  
  InsertItemPosition="LastItem"
  runat="server">
  <LayoutTemplate>
    <table cellpadding="2" border="1" runat="server" id="tblContacts" width="640px">
      <tr runat="server" id="itemPlaceholder" />
    </table>
    <asp:DataPager runat="server" ID="PeopleDataPager" PageSize="12">
      <Fields>
        <asp:NextPreviousPagerField 
          ShowFirstPageButton="true" ShowLastPageButton="true"
          FirstPageText="|&lt;&lt; " LastPageText=" &gt;&gt;|"
          NextPageText=" &gt; " PreviousPageText=" &lt; " />
      </Fields>
    </asp:DataPager>
  </LayoutTemplate>
  <ItemTemplate>
    <tr runat="server">
      <td valign="top">
        <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
        <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
      </td>
      <td>&nbsp;
        <asp:Label ID="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
      </td>
    </tr>
  </ItemTemplate>
  <InsertItemTemplate>
    <tr style="background-color:#D3D3D3">
      <td valign="top">
        <asp:Label runat="server" ID="FirstNameLabel" 
          AssociatedControlID="FirstNameTextBox" Text="First Name"/>
        <asp:TextBox ID="FirstNameTextBox" runat="server" 
          Text='<%#Bind("FirstName") %>' /><br />
        <asp:Label runat="server" ID="LastNameLabel" 
          AssociatedControlID="LastNameTextBox" Text="Last Name" />
        <asp:TextBox ID="LastNameTextBox" runat="server" 
          Text='<%#Bind("LastName") %>' /><br />
        <asp:Label runat="server" ID="EmailLabel" 
          AssociatedControlID="EmailTextBox" Text="Email" />
        <asp:TextBox ID="EmailTextBox" runat="server" 
          Text='<%#Bind("EmailAddress") %>' />
      </td>
      <td>
        <asp:LinkButton ID="InsertButton" runat="server" 
          CommandName="Insert" Text="Insert" />
      </td>
    </tr>
  </InsertItemTemplate>
</asp:ListView>

備註

使用 InsertItemTemplate 屬性,即可針對控制項中的 [插入] 專案,定義自訂使用者介面 (UI) ListViewUse the InsertItemTemplate property to define a custom user interface (UI) for the insert item in the ListView control. 這個 UI 會在顯示專案的開頭或結尾呈現。This UI is rendered either at the start or at the end of the displayed items. 您可以使用屬性來指定 UI 的呈現位置 InsertItemPositionYou specify where the UI is rendered by using the InsertItemPosition property.

InsertItemTemplate 範本通常包含輸入控制項,讓使用者輸入新記錄的值。The InsertItemTemplate template usually contains the input controls for the user to enter the values for a new record. 它通常也包含用來插入記錄和取消插入作業的按鈕或超連結。It also usually contains buttons or hyperlinks to insert the record and to cancel the insert operation.

若要以宣告方式指定自訂範本,請在專案 InsertItemTemplate 內加入元素 ListViewTo specify the custom template declaratively, add an InsertItemTemplate element inside the ListView element. 接著,您可以將範本的內容新增至專案 InsertItemTemplateYou can then add the contents of the template to the InsertItemTemplate element. 您可以使用雙向系結運算式,將欄位與輸入控制項產生關聯。You can associate a field with an input control by using a two-way binding expression. 插入記錄時, ListView 控制項會自動從相關聯的輸入控制項解壓縮域值。When a record is inserted, the ListView control automatically extracts the field value from the associated input control. 如需詳細資訊,請參閱資料系結 運算式總覽For more information, see Data-Binding Expressions Overview.

若要建立執行內建 [取消] 和 [插入] 作業的按鈕,請將按鈕控制項新增至範本。To create buttons that perform the built-in cancel and insert operations, add a button control to the template. 將其 CommandName 屬性設為下表所列的其中一個值。Set its CommandName property to one of the values listed in the following table.

按鈕類型Button type CommandName 值CommandName value
取消Cancel 退出"Cancel"
插入Insert 插入"Insert"

您可以使用屬性,控制 insert 專案在控制項中的位置 ListView InsertItemPositionYou can control the position of the insert item in the ListView control by using the InsertItemPosition property.

適用於

另請參閱