Creating Templates Programmatically in the DataGrid Control

Creating Templates Programmatically in the DataGrid Control

A DataGrid control can contain template columns in which you lay out the column by adding controls and literal text. Template columns provide you with substantially more flexibility than do bound columns, button columns, and hyperlink columns. As with templates for the DataList and Repeater controls, you can dynamically create templates for the DataGrid control. This allows you to define the contents, layout, and data of a column at run time.

Note   The procedures for working with a dynamic template column are similar to working with a dynamic template in a Repeater or DataList control. For general information on creating dynamic templates, see Creating Web Server Control Templates Dynamically.

The following are the differences between using a dynamic template in a DataGrid control and in the Repeater or DataList controls:

  • You do not create item templates for the grid itself; instead, you create them for a column in the grid.
  • There are slightly different templates for a DataGrid column than for a Repeater or DataList control. A DataGrid column does not include an alternating item or separator template. However, like the DataList control, it does include an EditItem template.

To create a dynamic template column

  1. Create a template class that implements the ITemplate interface of the System.Web.UI namespace. For details, see Creating Web Server Control Templates Dynamically.
  2. Optionally, pass into the class's constructor a value that the class can use to determine what type of template to create (ItemTemplate, EditItemTemplate, and so on).

    Typically, the template type is passed using a value from the ListItemType enumeration already defined in the DataGrid control.

  3. In the class, implement the InstantiateIn method (the only member of the ITemplate interface). This method provides a way to insert an instance of text and controls into the specified container.

    The following example shows a template class for a dynamic template column. The constructor accepts two parameters: the first parameter specifies the template type to create and the second parameter allows you to pass in the name of the column you are creating. Because this is a template for a DataGrid control, the class includes code to create an EditItem template containing a Textbox control.

    ' Visual Basic
    Private Class DataGridTemplate
       Implements ITemplate
       Dim templateType As ListItemType
       Dim columnName As String
    
       Sub New(ByVal type As ListItemType, ByVal ColName As String)
          templateType = type
          columnName = ColName
       End Sub
    
       Sub InstantiateIn(ByVal container As Control) _
          Implements ITemplate.InstantiateIn
          Dim lc As New Literal()
          Select Case templateType
             Case ListItemType.Header
                lc.Text = "<B>" & columnName & "</B>"
                container.Controls.Add(lc)
             Case ListItemType.Item
                lc.Text = "Item " & columnName
                container.Controls.Add(lc)
             Case ListItemType.EditItem
                Dim tb As New TextBox()
                tb.Text = ""
                container.Controls.Add(tb)
             Case ListItemType.Footer
                lc.Text = "<I>Footer</I>"
                container.Controls.Add(lc)
          End Select
       End Sub
    End Class
    
    // C#
    public class DataGridTemplate : ITemplate
    {
       ListItemType templateType;
       string columnName;
       
       public DataGridTemplate(ListItemType type, string colname)
       {
          templateType = type;
          columnName = colname;
       }
    
       public void InstantiateIn(System.Web.UI.Control container)
       {
          Literal lc = new Literal();
          switch(templateType)
          {
             case ListItemType.Header:
                lc.Text = "<B>" + columnName + "</B>";
                container.Controls.Add(lc);
                break;
             case ListItemType.Item:
                lc.Text = "Item " + columnName;
                container.Controls.Add(lc);
                break;
             case ListItemType.EditItem:
                TextBox tb = new TextBox();
                tb.Text = "";
                container.Controls.Add(tb);
                break;
             case ListItemType.Footer:
                lc.Text = "<I>" + columnName + "</I>";
                container.Controls.Add(lc);
                break;
          }
       }
    }

After you have created the class for a dynamic template column, you can use it to assign columns to the DataGrid control at run time.

To use dynamic template columns

  1. Create an instance of the TemplateColumn class.
  2. Create an instance of your dynamic template, passing it an item type value if appropriate.
  3. Assign the instance to one of the template properties of the TemplateColumn object you created in Step 1, such as ItemTemplate, EditItemTemplate, HeaderTemplate, and so on.

    The following example shows how to use the dynamic template column to add two columns to the DataGrid control. In this example, the templates are instantiated during the page load and before the control is bound to its data source.

    ' Visual Basic
    Private Sub Page_Load(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles MyBase.Load
       Dim tc1 As New TemplateColumn()
       tc1.HeaderTemplate = New _
          DataGridTemplate(ListItemType.Header, "Column1")
       tc1.ItemTemplate = New DataGridTemplate(ListItemType.Item, _
          "Column1")
       tc1.EditItemTemplate = New _
          DataGridTemplate(ListItemType.EditItem, "Column1")
       tc1.FooterTemplate = New _
          DataGridTemplate(ListItemType.Footer, "Column1")
       DataGrid1.Columns.Add(tc1)
       
       Dim tc2 As New TemplateColumn()
       tc2.HeaderTemplate = New _
          DataGridTemplate(ListItemType.Header, "Column2")
       tc2.ItemTemplate = New _
          DataGridTemplate(ListItemType.Item, "Column2")
       tc2.EditItemTemplate = New _
          DataGridTemplate(ListItemType.EditItem, "Column2")
       tc2.FooterTemplate = New _
          DataGridTemplate(ListItemType.Footer, "Column2")
       DataGrid1.Columns.Add(tc2)
       SqlDataAdapter1.Fill(DsCategories1)
       DataGrid1.DataBind()
    End Sub
    
    // C#
    private void Page_Load(object sender, System.EventArgs e)
    {
       TemplateColumn tc1 = new TemplateColumn();
       tc1.HeaderTemplate = new 
          DataGridTemplate(ListItemType.Header, "Column1");
       tc1.ItemTemplate = new 
          DataGridTemplate(ListItemType.Item, "Column1");
       tc1.EditItemTemplate = new 
          DataGridTemplate(ListItemType.EditItem, "Column1");
       tc1.FooterTemplate = new 
          DataGridTemplate(ListItemType.Footer, "Column1");
       DataGrid1.Columns.Add(tc1);
    
       TemplateColumn tc2 = new TemplateColumn();
       tc2.ItemTemplate = new 
          DataGridTemplate(ListItemType.Item, "Column2");
       tc2.HeaderTemplate = new 
          DataGridTemplate(ListItemType.Header, "Column2");
       tc2.EditItemTemplate = new 
          DataGridTemplate(ListItemType.EditItem, "Column2");
       tc2.FooterTemplate = new 
          DataGridTemplate(ListItemType.Footer, "Column2");
       DataGrid1.Columns.Add(tc2);
       sqlDataAdapter1.Fill(dsCategories1);
       DataGrid1.DataBind();
    }
See Also
基于SSM框架的智能家政保洁预约系统,是一个旨在提高家政保洁服务预约效率和管理水平的平台。该系统通过集成现代信息技术,为家政公司、家政服务人员和消费者提供了一个便捷的在线预约和管理系统。 系统的主要功能包括: 1. **用户管理**:允许消费者注册、登录,并管理他们的个人资料和预约历史。 2. **家政人员管理**:家政服务人员可以注册并更新自己的个人信息、服务类别和服务时间。 3. **服务预约**:消费者可以浏览不同的家政服务选项,选择合适的服务人员,并在线预约服务。 4. **订单管理**:系统支持订单的创建、跟踪和管理,包括订单的确认、完成和评价。 5. **评价系统**:消费者可以在家政服务完成后对服务进行评价,帮助提高服务质量和透明度。 6. **后台管理**:管理员可以管理用户、家政人员信息、服务类别、预约订单以及处理用户反馈。 系统采用Java语言开发,使用MySQL数据库进行数据存储,通过B/S架构实现用户与服务的在线交互。系统设计考虑了不同用户角色的需求,包括管理员、家政服务人员和普通用户,每个角色都有相应的权限和功能。此外,系统还采用了软件组件化、精化体系结构、分离逻辑和数据等方法,以便于未来的系统升级和维护。 智能家政保洁预约系统通过提供一个集中的平台,不仅方便了消费者的预约和管理,也为家政服务人员提供了一个展示和推广自己服务的机会。同时,系统的后台管理功能为家政公司提供了强大的数据支持和决策辅助,有助于提高服务质量和管理效率。该系统的设计与实现,标志着家政保洁服务向现代化和网络化的转型,为管理决策和控制提供保障,是行业发展中的重要里程碑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值