本文介绍了C#.net中动态网格视图中的TemplateField,ItemTemplate和EditItemTemplte的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TemplateField tf = new TemplateField();

tf.HeaderText =Product ID;

tf.ItemTemplate =







我正在尝试在动态gridview中编写templatefield,itemtemplate和EditItemTemplate。

但我还不是全部得到任何想法请任何帮助。我的迫切要求请任何人帮助我。提前谢谢

TemplateField tf = new TemplateField();
tf.HeaderText = "Product ID";
tf.ItemTemplate=



I am trying to write templatefield,itemtemplate and EditItemTemplate in Dynamic gridview.
But I am Not yet all getting any idea please any help. Its My urgent requirement please any one help me.Thanks in advance

推荐答案

public class TemplateGenerator : ITemplate // Class inheriting ITemplate
    {
        ListItemType type;
        string columnName;    
        public TemplateGenerator(ListItemType t, string cN)
        {           
           type = t;    
           columnName= cN;    
        }
        // Override InstantiateIn() method
        void ITemplate.InstantiateIn(System.Web.UI.Control container)
        {    
            switch (type)
            {
                case ListItemType.Item:    
                   HyperLink hyprLnk = new HyperLink();
                   hyprLnk.Target = "_blank"; //Optional.
                   hyprLnk.DataBinding+=new EventHandler(hyprLnk_DataBinding);
                   container.Controls.Add(hyprLnk);
                break;      
            }
        }    
 // The DataBinding event of your controls
 void hyprLnk_DataBinding(object sender, EventArgs e)
      {    
        HyperLink hyprlnk = (HyperLink)sender;
        GridViewRow container = (GridViewRow)hyprlnk.NamingContainer;
        object bindValue = DataBinder.Eval(container.DataItem,columnName);
      // Adding check in case Column allows null values
        if (bindValue != DBNull.Value) 
        {
            hyprlnk.Text = bindValue.ToString();
            hyprlnk.NavigateUrl = "http://www.google.com";
        }
     }










protected void GenerateGridViewColumnsDynamically()
        {
            // Create the TemplateField 
            TemplateField firstName = new TemplateField();
            firstName.HeaderText = "First_Name"; 
            firstName.ItemTemplate = new TemplateGenerator(ListItemType.Item,
                                                          "FirstName");
            // Showing boundField example just for more context
            BoundField lastName = new BoundField();
            lastName.DataField = "LastName";
            lastName.HeaderText = "Last_Name";
           // Add the Columns now
            MyGridView.Columns.Add(firstName);
            MyGridView.Columns.Add(lastName);
        }


这篇关于C#.net中动态网格视图中的TemplateField,ItemTemplate和EditItemTemplte的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 17:18