Aracılığıyla paylaş


TemplateField.ItemTemplate Özellik

Tanım

Bir öğeyi veriye bağlı denetimde görüntülemek için şablonu alır veya ayarlar.

public:
 virtual property System::Web::UI::ITemplate ^ ItemTemplate { 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 ItemTemplate { 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.ItemTemplate : System.Web.UI.ITemplate with get, set
Public Overridable Property ItemTemplate As ITemplate

Özellik Değeri

bir ITemplateiçindeki bir öğeyi TemplateFieldgörüntülemek için şablonu içeren bir -implemented nesnesi. Varsayılan değer, nullbu özelliğin ayarlanmadığını gösteren değeridir.

Öznitelikler

Örnekler

Aşağıdaki kod örneği, bir denetimdeki ItemTemplate bir alanın GridView öğeleri için özel şablon oluşturmak üzere özelliğinin TemplateField nasıl kullanılacağını gösterir. Şablon, alanın değerini bir RadioButtonList denetimde görüntüler.


<%@ 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 TitleGridView_RowDataBound (Object sender, GridViewRowEventArgs e)
  {
    
    // Get the RadioButtonList control from the row.
    RadioButtonList radio = (RadioButtonList)e.Row.FindControl("TypeList");
    
    // Select the appropriate option button based on the value
    // of the Type field for the row. In this example, the Type
    // field values are stored in the column in the 
    // GridView control.
    if (radio != null)
    {
      switch (e.Row.Cells[3].Text.Trim())
      {
        case "business":
          radio.SelectedIndex = 0; 
          break;

        case "mod_cook":
          radio.SelectedIndex = 1; 
          break;

        case "popular_comp":
          radio.SelectedIndex = 2; 
          break;

        case "psychology":
          radio.SelectedIndex = 3; 
          break;

        case "trad_cook":
          radio.SelectedIndex = 4; 
          break;

        default:
          radio.SelectedIndex = 5; 
          break;
      }
    }
    
  }
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TemplateField ItemTemplate Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>TemplateField ItemTemplate Example</h3>

      <!-- Populate the Columns collection declaratively. -->
      <!-- Create a custom TemplateField column that uses      -->
      <!-- two Label controls to display an author's first and -->
      <!-- last name in the same column.                       -->
      <asp:gridview id="TitleGridView" 
        datasourceid="TitleSqlDataSource" 
        autogeneratecolumns="false"
        onrowdatabound="TitleGridView_RowDataBound" 
        runat="server">
                
        <columns>
          
          <asp:boundfield datafield="title"
            headertext="Title"/>
          
          <asp:boundfield datafield="price"
            dataformatstring="{0:c}"
            headertext="Price"/>  
                  
          <asp:templatefield headertext="Type">
            <itemtemplate>
              <asp:radiobuttonlist id="TypeList"
                datasourceid="TypeSqlDataSource"
                datatextfield="type"
                enabled="false"  
                runat="server"/>  
            </itemtemplate>
          </asp:templatefield>
          
          <asp:boundfield datafield="type"/>
                
        </columns>
                
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Pubs sample database.                        -->
      <asp:sqldatasource id="TitleSqlDataSource"  
        selectcommand="SELECT [title], [price], [type] FROM [titles]"
        connectionstring="server=localhost;database=pubs;integrated security=SSPI"
        runat="server">
      </asp:sqldatasource>
      
      <asp:sqldatasource id="TypeSqlDataSource"  
        selectcommand="SELECT Distinct [type] FROM [titles]"
        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 TitleGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    
    ' Get the RadioButtonList control from the row.
    Dim radio As RadioButtonList = CType(e.Row.FindControl("TypeList"), RadioButtonList)
    
    ' Select the appropriate option button based on the value
    ' of the Type field for the row. In this example, the Type
    ' field values are stored in the column in the 
    ' GridView control.
    If Not radio Is Nothing Then

      Select Case e.Row.Cells(3).Text.Trim()

        Case "business"
          radio.SelectedIndex = 0

        Case "mod_cook"
          radio.SelectedIndex = 1

        Case "popular_comp"
          radio.SelectedIndex = 2

        Case "psychology"
          radio.SelectedIndex = 3

        Case "trad_cook"
          radio.SelectedIndex = 4

        Case Else
          radio.SelectedIndex = 5
      
      End Select
      
    End If
    
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TemplateField ItemTemplate Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>TemplateField ItemTemplate Example</h3>

      <!-- Populate the Columns collection declaratively. -->
      <!-- Create a custom TemplateField column that uses      -->
      <!-- two Label controls to display an author's first and -->
      <!-- last name in the same column.                       -->
      <asp:gridview id="TitleGridView" 
        datasourceid="TitleSqlDataSource" 
        autogeneratecolumns="false"
        onrowdatabound="TitleGridView_RowDataBound" 
        runat="server">
                
        <columns>
          
          <asp:boundfield datafield="title"
            headertext="Title"/>
          
          <asp:boundfield datafield="price"
            dataformatstring="{0:c}"
            headertext="Price"/>  
                  
          <asp:templatefield headertext="Type">
            <itemtemplate>
              <asp:radiobuttonlist id="TypeList"
                datasourceid="TypeSqlDataSource"
                datatextfield="type"
                enabled="false"  
                runat="server"/>  
            </itemtemplate>
          </asp:templatefield>
          
          <asp:boundfield datafield="type"/>
                
        </columns>
                
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Pubs sample database.                        -->
      <asp:sqldatasource id="TitleSqlDataSource"  
        selectcommand="SELECT [title], [price], [type] FROM [titles]"
        connectionstring="server=localhost;database=pubs;integrated security=SSPI"
        runat="server">
      </asp:sqldatasource>
      
      <asp:sqldatasource id="TypeSqlDataSource"  
        selectcommand="SELECT Distinct [type] FROM [titles]"
        connectionstring="server=localhost;database=pubs;integrated security=SSPI"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

Açıklamalar

Bir nesnedeki ItemTemplate öğeler TemplateField için görüntülenen özel içeriği belirtmek için özelliğini kullanın. Öğelerin nasıl işlendiğini belirten bir şablon oluşturarak içeriği tanımlayın.

Not

İsteğe bağlı olarak, veriye bağlı denetimdeki diğer her öğe için farklı bir görünüm oluşturmak için özelliğini özelliğiyle ItemTemplate birlikte tanımlayabilirsinizAlternatingItemTemplate.

Bir şablon belirtmek için, önce öğesinin açılış ve kapanış etiketleri arasında açma ve kapatma <ItemTemplate> etiketlerini yerleştirin <TemplateField> . Ardından, açma ve kapatma <ItemTemplate> etiketleri arasına özel içeriği ekleyin. İçerik düz metin kadar basit veya daha karmaşık olabilir (örneğin, şablona diğer denetimleri eklemek).

Şablonda tanımlanan bir denetime program aracılığıyla erişmek için, önce veriye bağlı denetimdeki hangi TableCell nesnenin denetimi içerdiğini belirleyin. Ardından, denetime Controls erişmek için nesnesinin TableCell koleksiyonunu kullanın. Denetimin FindControl belirtilen bir ID özelliği varsa, denetimi bulmak için nesnesinin yöntemini TableCell de kullanabilirsiniz.

Şunlara uygulanır

Ayrıca bkz.