zoukankan      html  css  js  c++  java
  • 动态创建模板列并绑定数据(GridView,Repeater,DataGrid)

    要创建动态模板,请创建以后需要时可实例化的模板类。

    创建模板类

      1. 创建实现 System.Web.UI.ITemplate 接口的新类。

      2. 您也可以将值传递到类的构造函数,类可以使用该值来确定要创建的模板类型(ItemTemplate、AlternatingItemTemplate 

    注意

    将模板类型传递到构造函数的类型安全方法是向构造函数添加类型为 ListItemType 的参数。ListItemType 枚举为 Repeater、DataList 和其他控件定义可能的模板类型。

    1. 在类中,实现 InstantiateIn 方法,该方法是 ITemplate 接口的成员。

      此方法提供将文本实例和控件实例插入指定容器的方法。

    2. 在 InstantiateIn 方法中,为模板项创建控件,设置其属性,然后将它们添加到父级的 Controls 集合。

      您可以通过传递到 InstantiateIn 方法的引用访问父控件。

      注意

      不能直接向 Controls 集合添加静态文本,但可以创建类似 Literal 控件或 LiteralControl 控件的控件,设置它们的 Text 属性,然后向父集合添加这些控件。

      下面的代码示例阐释完整的模板类,该类显示某些静态文本(“Item number:”)和计数器。计数器是名为 itemcount 的静态值(在 Visual Basic 中为共享值),在每次创建新项时都会递增。该类定义一个接受 ListItemType 枚举值以指示所创建模板类型的显式构造函数。根据所创建的模板类型,代码创建不同类型的控件并将其添加到父控件的 Controls 集合。最终结果是一个 HTML 表,其中的交替项模板具有不同的背景色。

      public class MyTemplate : ITemplate
      {
          static int itemcount = 0;
          ListItemType templateType;
      
          public MyTemplate(ListItemType type)
          {
              templateType = type;
          }
      
          public void InstantiateIn(System.Web.UI.Control container)
          {
              Literal lc = new Literal();
              switch (templateType)
              {
                  case ListItemType.Header:
                      lc.Text = "<TABLE border=1><TR><TH>Items</TH></TR>";
                      break;
                  case ListItemType.Item:
                      lc.Text = "<TR><TD>Item number: " + itemcount.ToString() +
                         "</TD></TR>";
                      break;
                  case ListItemType.AlternatingItem:
                      lc.Text = "<TR><TD bgcolor=lightblue>Item number: " +
                         itemcount.ToString() + "</TD></TR>";
                      break;
                  case ListItemType.Footer:
                      lc.Text = "</TABLE>";
                      break;
              }
              container.Controls.Add(lc);
              itemcount += 1;
          }
      }

      使用动态模板

      有了可用的动态模板,就可以在代码中将其实例化了。

      创建动态模板

      1. 创建动态模板的实例,如果合适的话,将一个项类型值传递给它。

      2. 将该实例分配给模板化控件的模板属性之一,如 ItemTemplate、AlternatingItemTemplate 和 HeaderTemplate 属性。

        下面的代码示例演示如何与 Repeater 控件一起使用动态模板。在此示例中,在加载页时,而且是在控件绑定到数据源之前,将模板实例化。

        private void Page_Load(object sender, System.EventArgs e)
        {
            SqlConnection conn = 
              new SqlConnection(ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString);
        
            SqlDataAdapter sqlDataAdapter1;
            DataSet dsCategories1;
        
            sqlDataAdapter1 = new SqlDataAdapter("SELECT CategoryID, CategoryName FROM Categories", conn);
            dsCategories1 = new DataSet();
                    
            Repeater1.HeaderTemplate = new MyTemplate(ListItemType.Header);
            Repeater1.ItemTemplate = new MyTemplate(ListItemType.Item);
            Repeater1.AlternatingItemTemplate =
               new MyTemplate(ListItemType.AlternatingItem);
            Repeater1.FooterTemplate = new MyTemplate(ListItemType.Footer);
            sqlDataAdapter1.Fill(dsCategories1, "Categories");
            Repeater1.DataSource = dsCategories1.Tables["Categories"];
            Repeater1.DataBind();
        }

        要将数据绑定添加到动态模板,必须执行如下操作:

        • 将数据绑定事件处理程序添加到您在模板中创建的控件。

        • 创建您要被绑定的处理程序。在该处理程序中,获取您要被绑定的数据并将其分配给要被绑定控件的相应属性。

        添加数据绑定事件处理程序

        • 在动态模板中创建控件后,添加对控件的 DataBinding 事件的事件处理程序的引用。(接下来您将创建事件处理程序)。

          下面的代码示例演示了模板类的一部分,该部分阐释了如何将新创建的控件绑定到名为 TemplateControl_DataBinding 的方法:

           
          case ListItemType.Item:
            
          lc.Text = "<TR><TD>";
          lc.DataBinding += new EventHandler(TemplateControl_DataBinding);
          break;

          在上例中,您添加到文本控件的 Text 属性的文本不同于前一示例。该示例只包含了项模板的表行和单元格的开头。您将在数据绑定事件处理程序中填写单元格和行。

        下一步是创建事件处理程序,在控件处于数据绑定状态时将调用该事件处理程序。

        二、在我自己的项目中使用的控件是GridView,动态模板列中使用的是LinkButton控件,创建的过程大概与上面相同,如下:

        创建模板类 

        public class GaugeValueTemplate : ITemplate
            {
                #region ITemplate 成员
        
                public void InstantiateIn(Control container)
                {
                    LinkButton lbGaugeMakeValue = new LinkButton();
                    lbGaugeMakeValue.ID = "LB_GaugeMakeValue";
                    lbGaugeMakeValue.DataBinding += new EventHandler(lbGaugeMakeValue_DataBinding);
                    lbGaugeMakeValue.CommandName = "GaugeMakeValue";
        
                    container.Controls.Add(lbGaugeMakeValue);
                }
        
                void lbGaugeMakeValue_DataBinding(object sender, EventArgs e)
                {
                    LinkButton lbGaugeMakeValue = (LinkButton)sender;
                    GridViewRow container = (GridViewRow)lbGaugeMakeValue.NamingContainer;
        
                   //绑定GaugeMakeValue字段的值
                   lbGaugeMakeValue.Text = DataBinder.Eval(container.DataItem, "GaugeMakeValue").ToString();
                }

        使用模板 

        TemplateField GaugeMakeValueColumn = new TemplateField();
                    GaugeMakeValueColumn.HeaderText = "指标制定值";
                    GaugeMakeValueColumn.ItemTemplate = new GaugeValueTemplate();

        从这里可以看出GridView与Repeater控件中的区别在于:

        Repeater:RepeaerItem container = (RepeaterItem)lc.NamingContainer;
            lc.Text += DataBinder.Eval(container.DataItem, "CategoryName");

        GridView:GridViewRow container = (GridViewRow)lbGaugeMakeValue.NamingContainer;

                   //绑定GaugeMakeValue字段的值
                   lbGaugeMakeValue.Text = DataBinder.Eval(container.DataItem, "GaugeMakeValue").ToString();

        三、关于DataGrid的方法,可参见http://www.cnblogs.com/lovecherry/archive/2005/03/25/125526.html

      3. 为 DataBinding 事件创建处理程序

          1. 创建属于模板类的方法,它还是该类的其他方法(如 InstantiateIn)的对等方法。处理程序的名称必须与早期绑定事件时使用的名称相匹配。该方法应该具有以下签名:

            private void TemplateControl_DataBinding(object sender,
            System.EventArgs e)
             
          2. 通过执行以下操作获取对包含数据的 DataItem 对象的引用:

            1. 获取对模板项的引用。创建变量来保存该引用,然后将从控件的 NamingContainer 属性获取的值分配给它。

            2. 使用该引用来获取命名容器的(模板项的)DataItem 属性。

            3. 从 DataItem 对象提取单个数据元素(例如数据列),并使用它来设置您要绑定的控件的属性。

              下面的代码示例阐释在动态模板内执行数据绑定的一种方式。它演示了在 Repeater 控件的模板中创建的 Literal 控件的完整数据绑定事件处理程序。

        private void TemplateControl_DataBinding(object sender,
        System.EventArgs e)
        {
            
            Literal lc;
        
            lc = (Literal)sender;
            RepeaterItem container = (RepeaterItem)lc.NamingContainer;
            lc.Text += DataBinder.Eval(container.DataItem, "CategoryName");
            lc.Text += "</TD></TR>";
        }
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Web.UI;
        using System.Web.UI.WebControls;
        
        namespace Xys.Remp.WF.UI
        {
            public class WorkFlowTraceTemplate : ITemplate
            {
                ListItemType templateType;
        
                public WorkFlowTraceTemplate(ListItemType type)
                {
                    templateType = type;
                }
        
                public void InstantiateIn(System.Web.UI.Control container)
                {
                    Literal lc = new Literal();
                    switch (templateType)
                    {
                        case ListItemType.Header:
                            lc.Text = "<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\" class=\"addformcontainer\"><tr><td>节点编号</td><td>节点名称</td><td>处理人</td><td>审批意见</td><td>时间</td></tr>";
                            break;
                        case ListItemType.Item:
                            lc.DataBinding += new EventHandler(TemplateControl_DataBinding);
                            break;
                            break;
                        case ListItemType.Footer:
                            lc.Text = "</table>";
                            break;
                    }
                    container.Controls.Add(lc);
                }
        
                private void TemplateControl_DataBinding(object sender,
        System.EventArgs e)
                {
                    Literal lc;
                    lc = (Literal)sender;
                    RepeaterItem container = (RepeaterItem)lc.NamingContainer;
                    lc.Text = "  <tr><td>" + DataBinder.Eval(container.DataItem, "NodeID") + "</td><td>" + DataBinder.Eval(container.DataItem, "NodeName") + "</td><td>" + DataBinder.Eval(container.DataItem, "CurrentPersonName") + "</td><td>" + DataBinder.Eval(container.DataItem, "Remark") + "</td><td>" + DataBinder.Eval(container.DataItem, "HandleTime") + "</td></tr>";
                }
            }
        }

        以上文章参考http://www.cnblogs.com/50614090/archive/2011/08/19/2145399.html

        http://www.cnblogs.com/guiliangfeng/archive/2009/04/19/1439235.html

    如果这篇文章对您有帮助,您可以打赏我

    技术交流QQ群:15129679

  • 相关阅读:
    TXNLP 01-09
    王某的NLP之路前言
    回归模型(一) 线性回归
    回归模型(三) 岭回归
    回归模型(二) 逻辑回归
    hadoop中的JournalNode
    systemctl详解
    HADOOP HA 报错
    集成学习的不二法门bagging、boosting和三大法宝<结合策略>平均法,投票法和学习法(stacking)
    阿里云集群搭建
  • 原文地址:https://www.cnblogs.com/yeminglong/p/2823711.html
Copyright © 2011-2022 走看看