通过


TemplateColumn 类

定义

表示控件的 DataGrid 列类型,可用于自定义列中控件的布局。

public ref class TemplateColumn : System::Web::UI::WebControls::DataGridColumn
public class TemplateColumn : System.Web.UI.WebControls.DataGridColumn
type TemplateColumn = class
    inherit DataGridColumn
Public Class TemplateColumn
Inherits DataGridColumn
继承
TemplateColumn

示例

下面的代码示例演示如何使用 TemplateColumn 类在控件中使用 DataGrid 自定义布局创建列。


<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
   <script runat="server">

      DataTable Store = new DataTable();
      DataView StoreView;  

      void Page_Load(Object sender, EventArgs e) 
      {
         if(Session["StoreData"] == null)
         {
            DataRow dr;
 
            Store = new DataTable();      

            Store.Columns.Add(new DataColumn("Tax", typeof(String)));
            Store.Columns.Add(new DataColumn("Item", typeof(String)));
            Store.Columns.Add(new DataColumn("Price", typeof(String)));

            Session["StoreData"] = Store;
            
            // Create sample data.
            for (int i = 1; i <= 4; i++) 
            {
               dr = Store.NewRow();

               dr[0] = "0.0%";
               dr[1] = "Item " + i.ToString();
               dr[2] = (1.23 * (i + 1)).ToString();
 
               Store.Rows.Add(dr);
            }       

         }
         else
            Store = (DataTable)Session["StoreData"];

         StoreView = new DataView(Store);
         StoreView.Sort="Item";

         if(!IsPostBack)                    
            BindGrid();
                   
      }

      void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs e) 
      {
         MyDataGrid.EditItemIndex = e.Item.ItemIndex;
         BindGrid();
      }

      void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs e) 
      {
         MyDataGrid.EditItemIndex = -1;
         BindGrid();
      }

      void MyDataGrid_Update(Object sender, DataGridCommandEventArgs e) 
      {
         // Get the text box that contains the price to edit. 
         // For bound columns the edited value is stored in a text box.
         // The text box is the first control in the Controls collection.
         TextBox priceText = (TextBox)e.Item.Cells[3].Controls[0];

         // Get the check box that indicates whether to include tax from the 
         // TemplateColumn. Notice that in this case, the check box control is
         // second control in the Controls collection.
         CheckBox taxCheck = (CheckBox)e.Item.Cells[2].Controls[1];

         String item = e.Item.Cells[1].Text;
         String price = priceText.Text;
       
         DataRow dr;

         // With a database, use an update command.  Since the data source is 
         // an in-memory DataTable, delete the old row and replace it with a new one.

         // Remove old entry.
         StoreView.RowFilter = "Item='" + item + "'";
         if (StoreView.Count > 0)
            StoreView.Delete(0);
         StoreView.RowFilter = "";
 
         // Add new entry.
         dr = Store.NewRow();

         if (taxCheck.Checked)
            dr[0] = "8.6%";
         else 
            dr[0] = "0.0%";
         dr[1] = item;
         dr[2] = price;
         Store.Rows.Add(dr);

         MyDataGrid.EditItemIndex = -1;
         BindGrid();
      }

      void BindGrid() 
      {
         MyDataGrid.DataSource = StoreView;
         MyDataGrid.DataBind();
      }

   </script>

<head runat="server">
    <title>TemplateColumn Example</title>
</head>
<body>

   <form id="form1" runat="server">

      <h3>TemplateColumn Example</h3>

      <asp:DataGrid id="MyDataGrid" runat="server"
           BorderColor="black"
           CellPadding="2"        
           OnEditCommand="MyDataGrid_Edit"
           OnCancelCommand="MyDataGrid_Cancel"
           OnUpdateCommand="MyDataGrid_Update"
           ShowFooter="True"
           AutoGenerateColumns="false">

         <Columns>

            <asp:EditCommandColumn
                 EditText="Edit"
                 CancelText="Cancel"
                 UpdateText="Update"
                 ItemStyle-Wrap="false"
                 HeaderText="Edit Controls"/>

            <asp:BoundColumn HeaderText="Description" 
                 ReadOnly="true" 
                 DataField="Item"/>

            <asp:TemplateColumn>

               <HeaderTemplate>
                  <b> Tax </b>
               </HeaderTemplate>

               <ItemTemplate>
                  <asp:Label
                       Text='<%# DataBinder.Eval(Container.DataItem, "Tax") %>'
                       runat="server"/>
               </ItemTemplate>

               <EditItemTemplate>

                  <asp:CheckBox
                       Text="Taxable" 
                       runat="server"/>

               </EditItemTemplate>

               <FooterTemplate>
                  <asp:HyperLink id="HyperLink1"
                       Text="Microsoft"
                       NavigateUrl="http://www.microsoft.com"
                       runat="server"/>
               </FooterTemplate>

            </asp:TemplateColumn>

            <asp:BoundColumn HeaderText="Price" 
                 DataField="Price"/>

         </Columns>

      </asp:DataGrid>

   </form>

</body>
</html>

<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
   <script runat="server">

      Private Store As DataTable = New DataTable()
      Private StoreView As DataView  

      Sub Page_Load(sender As Object, e As EventArgs) 
     
         If IsNothing(Session("StoreData")) Then 
         
            Dim dr As DataRow
            Dim i As Integer
 
            Store = New DataTable()      

            Store.Columns.Add(New DataColumn("Tax", GetType(String)))
            Store.Columns.Add(New DataColumn("Item", GetType(String)))
            Store.Columns.Add(New DataColumn("Price", GetType(String)))

            Session("StoreData") = Store
            
            ' Create sample data.
            For i = 1 to 4 
    
               dr = Store.NewRow()

               dr(0) = "0.0%"
               dr(1) = "Item " & i.ToString()
               dr(2) = (1.23 * (i + 1)).ToString()
 
               Store.Rows.Add(dr)

            Next i       

         Else
            Store = Session("StoreData")

         End If

         StoreView = New DataView(Store)
         StoreView.Sort="Item"

         If Not IsPostBack Then                    
            BindGrid()
         End If
                   
      End Sub

      Sub MyDataGrid_Edit(sender As Object, e As DataGridCommandEventArgs) 
      
         MyDataGrid.EditItemIndex = e.Item.ItemIndex
         BindGrid()

      End Sub

      Sub MyDataGrid_Cancel(sender As Object, e As DataGridCommandEventArgs) 
      
         MyDataGrid.EditItemIndex = -1
         BindGrid()

      End Sub

      Sub MyDataGrid_Update(sender As Object, e As DataGridCommandEventArgs) 
      
         ' Get the text box that contains the price to edit. 
         ' For bound columns the edited value is stored in a text box.
         ' The text box is the first control in the Controls collection.
         Dim priceText As TextBox = e.Item.Cells(3).Controls(0)

         ' Get the check box that indicates whether to include tax from the 
         ' TemplateColumn. Notice that in this case, the check box control is
         ' second control in the Controls collection.
         Dim taxCheck As CheckBox = e.Item.Cells(2).Controls(1)

         Dim item As String = e.Item.Cells(1).Text
         Dim price As String = priceText.Text
       
         Dim dr As DataRow

         ' With a database, use an update command.  Since the data source is 
         ' an in-memory DataTable, delete the old row and replace it with a new one.

         ' Remove old entry.
         StoreView.RowFilter = "Item='" & item & "'"
         If StoreView.Count > 0 Then
            StoreView.Delete(0)
         End If
         StoreView.RowFilter = ""
 
         ' Add new entry.
         dr = Store.NewRow()

         If taxCheck.Checked Then
            dr(0) = "8.6%"
         Else 
            dr(0) = "0.0%"
         End If
         dr(1) = item
         dr(2) = price
         Store.Rows.Add(dr)

         MyDataGrid.EditItemIndex = -1
         BindGrid()

      End Sub

      Sub BindGrid() 
      
         MyDataGrid.DataSource = StoreView
         MyDataGrid.DataBind()
      
      End Sub

   </script>

<head runat="server">
    <title>TemplateColumn Example</title>
</head>
<body>

   <form id="form1" runat="server">

      <h3>TemplateColumn Example</h3>

      <asp:DataGrid id="MyDataGrid" runat="server"
           BorderColor="black"
           CellPadding="2"        
           OnEditCommand="MyDataGrid_Edit"
           OnCancelCommand="MyDataGrid_Cancel"
           OnUpdateCommand="MyDataGrid_Update"
           ShowFooter="True"
           AutoGenerateColumns="false">

         <Columns>

            <asp:EditCommandColumn
                 EditText="Edit"
                 CancelText="Cancel"
                 UpdateText="Update"
                 ItemStyle-Wrap="false"
                 HeaderText="Edit Controls"/>

            <asp:BoundColumn HeaderText="Description" 
                 ReadOnly="true" 
                 DataField="Item"/>

            <asp:TemplateColumn>

               <HeaderTemplate>
                  <b> Tax </b>
               </HeaderTemplate>

               <ItemTemplate>
                  <asp:Label
                       Text='<%# DataBinder.Eval(Container.DataItem, "Tax") %>'
                       runat="server"/>
               </ItemTemplate>

               <EditItemTemplate>

                  <asp:CheckBox
                       Text="Taxable" 
                       runat="server"/>

               </EditItemTemplate>

               <FooterTemplate>
                  <asp:HyperLink id="HyperLink1"
                       Text="Microsoft"
                       NavigateUrl="http://www.microsoft.com"
                       runat="server"/>
               </FooterTemplate>

            </asp:TemplateColumn>

            <asp:BoundColumn HeaderText="Price" 
                 DataField="Price"/>

         </Columns>

      </asp:DataGrid>

   </form>

</body>
</html>

注解

TemplateColumn使用控件中的DataGrid列类型创建具有自定义控件布局的列。 可以使用列的标题部分、页脚部分和项部分分别使用HeaderTemplateFooterTemplateItemTemplate属性来提供自定义外观。 还可以通过设置EditItemTemplate属性来控制正在编辑的TemplateColumn项在对象中的显示方式。

构造函数

名称 说明
TemplateColumn()

初始化 TemplateColumn 类的新实例。

属性

名称 说明
DesignMode

获取一个值,该值指示列是否处于设计模式。

(继承自 DataGridColumn)
EditItemTemplate

获取或设置用于显示为在对象中编辑而选择的项的 TemplateColumn 模板。

FooterStyle

获取列的页脚部分的样式属性。

(继承自 DataGridColumn)
FooterTemplate

获取或设置用于显示对象的页脚部分的 TemplateColumn 模板。

FooterText

获取或设置列的页脚部分中显示的文本。

(继承自 DataGridColumn)
HeaderImageUrl

获取或设置要显示在列标题部分中的图像的位置。

(继承自 DataGridColumn)
HeaderStyle

获取列标题部分的样式属性。

(继承自 DataGridColumn)
HeaderTemplate

获取或设置用于显示对象的标题部分的 TemplateColumn 模板。

HeaderText

获取或设置列标题部分中显示的文本。

(继承自 DataGridColumn)
IsTrackingViewState

获取一个值,该值确定是否 DataGridColumn 将对象标记为保存其状态。

(继承自 DataGridColumn)
ItemStyle

获取列的项单元格的样式属性。

(继承自 DataGridColumn)
ItemTemplate

获取或设置用于在对象中显示数据项的 TemplateColumn 模板。

Owner

DataGrid获取列所属的控件。

(继承自 DataGridColumn)
SortExpression

获取或设置选择列进行排序时要传递给 OnSortCommand(DataGridSortCommandEventArgs) 方法的字段或表达式的名称。

(继承自 DataGridColumn)
ViewState

StateBag获取允许从DataGridColumn类派生的列存储其属性的对象。

(继承自 DataGridColumn)
Visible

获取或设置一个值,该值指示该列是否在控件中 DataGrid 可见。

(继承自 DataGridColumn)

方法

名称 说明
Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
Initialize()

提供基实现,用于将派生自类的 DataGridColumn 列重置为其初始状态。

(继承自 DataGridColumn)
InitializeCell(TableCell, Int32, ListItemType)

TableCell调用对象的基类来初始化实例,然后向单元格应用一个ListItemType

LoadViewState(Object)

加载对象的状态 DataGridColumn

(继承自 DataGridColumn)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
OnColumnChanged()

调用该方法 OnColumnsChanged()

(继承自 DataGridColumn)
SaveViewState()

保存对象的 DataGridColumn 当前状态。

(继承自 DataGridColumn)
ToString()

返回列的字符串表示形式。

(继承自 DataGridColumn)
TrackViewState()

导致跟踪服务器控件的视图状态更改,以便它们可以存储在服务器控件的对象 StateBag 中。

(继承自 DataGridColumn)

显式接口实现

名称 说明
IStateManager.IsTrackingViewState

获取一个值,该值指示列是否跟踪视图状态更改。

(继承自 DataGridColumn)
IStateManager.LoadViewState(Object)

加载以前保存的状态。

(继承自 DataGridColumn)
IStateManager.SaveViewState()

返回包含状态更改的对象。

(继承自 DataGridColumn)
IStateManager.TrackViewState()

开始跟踪状态更改。

(继承自 DataGridColumn)

适用于

另请参阅