TemplateField.InsertItemTemplate 屬性

定義

取得或設定顯示 TemplateField 物件中處於插入模式之項目的樣板。

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

ITemplate 實作的物件,包含顯示 TemplateField 中處於插入模式之項目的樣板。 預設值為 null,表示這個屬性尚未設定。

屬性

範例

下列程式碼範例示範如何使用 InsertItemTemplate 屬性,在 控制項的欄位資料列中 DetailsView ,為插入模式中的 TemplateField 專案建立自訂範本。 此範本會顯示 DropDownList 控制項,允許使用者從預先定義的清單中選取值。


<%@ 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 專案所顯示的自訂內容。 藉由建立範本來定義內容,以指定插入模式中的專案呈現方式。

若要指定範本,請先在元素的開頭和結束記號之間放置開頭和結尾 <InsertItemTemplate> 標記 <TemplateField> 。 接下來,在開頭和結尾 <InsertItemTemplate> 標記之間新增自訂內容。 內容可以像純文字一樣簡單,或更複雜的 (在範本中內嵌其他控制項,例如) 。

若要以程式設計方式存取範本中定義的控制項,請先判斷資料繫結控制項中的哪個 TableCell 物件包含 控制項。 接下來,使用 Controls 物件的集合 TableCell 來存取 控制項。 如果控制項已 ID 指定屬性,您也可以使用 FindControl 物件的 方法來 TableCell 尋找控制項。

注意

並非所有資料繫結控制項都支援此範本 此範本僅受資料繫結控制項支援,可讓您插入記錄,例如 DetailsView 控制項。

適用於

另請參閱