Dynamically Adding Template columns to a GridView
来源:互联网 发布:网络信息采集与编辑 编辑:程序博客网 时间:2024/04/30 19:41
Introduction
Many a times we wonder how to add template column dynamically. This might be the case when number of columns, column type etc. cannot be decided during design time. For example, if the data source of a Gridview control will be retrieved from a Business layer, during designing the Gridview user interface, developer can not determine no. of columns and other things related to column description. In such a case, Gridview will be configured to generate columns automatically at runtime. Adding template column at runtime was not possible in earlier version of .Net i.e. 1.0. With the new version .NET 2.0 onwards, there is a facility to add template column at runtime dynamically.
For the above purpose, I have created a class 'DynamicTemplate' inheriting 'ITemplate' interface.
public class DynamicTemplate : System.Web.UI.ITemplate
Now the very first step is to implement constructor for the class:
public
DynamicTemplate(System.Web.UI.WebControls.ListItemType type)
{
templateType = type;
}
Here I have passed Listitemtype as a parameter to the constructor, which will define the type of current template. i.e. template is Header, Item, AlternatingItem or Footer.
Next, for defining the type of control template column will be displaying, I have added following method:
public void AddControl(WebControl wbControl,String
BindPropertyName, String
BindExpression)
{
htControls.Add(htControls.Count, wbControl);
htBindPropertiesNames.Add(htBindPropertiesNames.Count, BindPropertyName);
htBindExpression.Add(htBindExpression.Count, BindExpression);
}
Here we need to pass 3 parameters, wbcontrol will be reference to any web control which we want our template column to display, BindPropertyName will be the property name of the control to be binded i.e. Text property for a textbox, label control. BindExpression will be the field name or any valid Bind expression to be evaluated and assigned to the BindProperty.
Now for providing template column funtionality, we need to implement 'InstantiateIn' method. This method will be called for every row of gridview before Binding occurs.
Collapse
public void InstantiateIn(System.Web.UI.Control container)
{
PlaceHolder ph = new PlaceHolder();
for (int i = 0; i < htControls.Count; i++)
{
//clone control
Control cntrl = CloneControl((Control)htControls);
switch (templateType)
{
case ListItemType.Header:
break;
case ListItemType.Item:
ph.Controls.Add(cntrl);
break;
case ListItemType.AlternatingItem:
ph.Controls.Add(cntrl);
ph.DataBinding += new EventHandler(Item_DataBinding);
break;
case ListItemType.Footer:
break;
}
}
ph.DataBinding += new EventHandler(Item_DataBinding);
container.Controls.Add(ph);
}
Here we first clone the webcontrol so that a new copy can be created and then we add this new copy to the desired placeholder.
Next, we want to implement this class for adding template column dynamically:
TemplateField t = new TemplateField();
DynamicTemplate mt = new DynamicTemplate(ListItemType.Item);
TextBox t1 = new TextBox();
t1.ID = "txt";
t1.Visible = true;
t1.Text = "1";
mt.AddControl(t1, "Text", "Sno");
Here we are creating a template column which will display a textbox. And text property of this textbox will be binded with the Sno data field of configured data source.
A complete implementation of the above class is attached with this article which is quite self explanatory and simple.
- Dynamically Adding Template columns to a GridView
- Dynamically Creating Bound and Template Columns in GridView
- Dynamically Adding Controls to a Web Page...
- Dynamically creating bound and template columns in GridView using ASP.Net
- apache camle template , Adding Routes dynamically
- CodeSnip: How to Create a BoundField Dynamically Using GridView
- Dynamically adding tab panels to tab containers
- Creating GridView Columns Dynamically (Part 1)--动态创建GridView的列(一)
- How to delay a method call dynamically?
- How to Dynamically Slice a Convex Shape
- Creating DataGrid Templated Columns Dynamically -
- Adding Content to a Document
- Adding a Floating Panel to a Window
- [R] adding a title to a histogram
- Adding a Single Task to a Queue
- How to return a long columns size
- Creating DataGrid Templated Columns Dynamically - Part I
- Creating DataGrid Templated Columns Dynamically - Part II
- Ubuntu C/C++编译环境安装配置
- ASP.NET 常用代码 3
- ASP.NET 常用代码 4
- 什么是VPN技术
- 跨越5000年的爱情
- Dynamically Adding Template columns to a GridView
- Oracle关于时间/日期的操作
- xp无法访问局域网的解决方案
- spring+ibatis开发实践
- 既然大家都log,那我也來lo他一g
- 配置 Apache + Tomcat WebService环境
- CSDN总裁蒋涛 经验分享:漫谈创业和管理-程序员5大思维障碍
- 三步堵死SQL注入漏洞
- GIF文档