动态模板类使用 ITemplate 接口 GridView动态模板
2009-09-19 09:43
381 查看
GridView中动态添加模板,这个操作不复杂,主要调用了ITemplate 接口及其方法InstantiateIn,可能会用到TemplateField类。以GridView中动态添加模板列为例。 首先根据创建一个实现ITemplate 接口的类,并重写InstantiateIn方法: /// <summary> /// 模板类 /// </summary> public class GridViewLiteralTemplated : ITemplate { private DataControlRowType templateType;//行类型 private string columnName;// 列名称 private string dataField;// 字段名称 private string literalID;// Literal 控件ID private bool showOrNot;// 是否显示列标题 public GridViewLiteralTemplated(DataControlRowType type, string colname, string datafield, string litID, bool showYN) { templateType = type; columnName = colname; dataField = datafield; literalID = litID; showOrNot = showYN; } /// <summary> /// 重写InstantiateIn方法,处理行类型 /// </summary> /// <param name="container"></param> public void InstantiateIn(System.Web.UI.Control container) { switch (templateType) { case DataControlRowType.Header: Literal lc = new Literal(); lc.Text = columnName; lc.Visible = showOrNot; container.Controls.Add(lc); break; case DataControlRowType.DataRow: Literal lit = new Literal(); lit.ID = literalID; lit.Visible = showOrNot; container.Controls.Add(lit); break; default: break; } } } 说明:行类型是区别是标题行还是记录行;列名称是标题行的列名称; 字段名称是与查询语句中对应的名称,用于数据绑定;Literal 控件ID是前台显示的载体;是否显示列标题则是根据需要来显示或隐藏某些列。 类写好后,就可以来调用,根据需求来确定参数的值。比如: UserDepartPermissionGV.Columns.Clear(); TemplateField tfID = new TemplateField(); tfID.HeaderTemplate = new GridViewLiteralTemplated(DataControlRowType.Header, "区划编码", "FID", "", false); tfID.ItemTemplate = new GridViewLiteralTemplated(DataControlRowType.DataRow, "", "", "AreaID", false); UserDepartPermissionGV.Columns.Add(tfID); TemplateField tfName = new TemplateField(); tfName.HeaderTemplate = new GridViewLiteralTemplated(DataControlRowType.Header, "区划名称", "FNAME", "", true); tfName.ItemTemplate = new GridViewLiteralTemplated(DataControlRowType.DataRow, "", "", "DepartmentID", true); UserDepartPermissionGV.Columns.Add(tfName); 这种写法如果要手写许多列,则相当麻烦,适合从数据库中批量处理。 前台页面的GRIDVIEW : <asp:GridView ID="UserDepartPermissionGV" runat="server" AutoGenerateColumns="false" OnRowDataBound="UserDepartPermissionGV_RowDataBound" Width="1000px"> </asp:GridView> 动态模板列实现方法有几种,你可以根据需求写出自己的模板列。
相关文章推荐
- 动态模板类使用 ITemplate 接口 GridView动态模板
- 动态模板类使用 ITemplate 接口 GridView动态模板
- 动态模板类使用 ITemplate 接口 GridView动态模板
- 如何使用动态创建模板列并绑定数据(GridView,Repeater,DataGrid)
- C++ - 模板类使用成员函数模板(member function template)处理隐式(implicit)转换
- 模板类使用成员函数模板(member function template)处理隐式(implicit)转换
- 如何使用动态创建模板列并绑定数据(GridView,Repeater,DataGrid)
- GridView动态创建模板列templateField
- 如何使用动态创建模板列并绑定数据(GridView,Repeater,DataGrid)
- 实现System.Web.UI.ITemplate接口来动态设置控件模板
- GridView中动态生成ItemTemplate模板列
- 解决 "Use a cube template" is dimmed (disabled) "使用立方模板" 选项不可选(变灰)(被禁用)的问题
- ASP 模板引擎,ASP 模板类 (Taihom.Template.class)
- template-web.js模板引擎的使用之初级篇
- ASP.NET2.0中的GRIDVIEW控件在使用TemplateField中的LinkButton时如何在RowCommand事件中找到当前行index的方法
- 对kingthy创作的Vtemplate模板引擎的使用心得
- ASP.NET 2.0数据教程之十二:在GridView控件中使用TemplateField(转)
- ASP.NET 2.0中动态添加 GridView 模板列的例子
- 使用Using System.Data.OleDb; 连接数据库,又一种动态绑定GridView显示数据。
- 使用DataTable动态绑定GridView