<%@ Page language="C#" %>
<script runat="server">
// Create a dynamic template column
public class GridViewTextTemplate : System.Web.UI.ITemplate
{
private DataControlRowType templateType;
private string columnName;
private string cId;
public GridViewTextTemplate(DataControlRowType type, string colname, string controlId)
{
templateType = type;
columnName = colname;
cId = controlId;
}
public void InstantiateIn(System.Web.UI.Control container)
{
// Create the content for the different row types.
switch (templateType)
{
case DataControlRowType.Header:
Literal myHeadLiteral = new Literal();
// Create the controls and set id properties to put in the header
myHeadLiteral.ID = cId;
myHeadLiteral.Text = "<B>" + columnName + "</B>";
container.Controls.Add(myHeadLiteral);
break;
case DataControlRowType.DataRow:
// Create the controls and set id properties to put in a data row
TextBox myTextBox = new TextBox();
myTextBox.ID = cId;
myTextBox.DataBinding += new EventHandler(this.TextBoxDataBinding);
container.Controls.Add(myTextBox);
break;
default:
// Insert code to handle unexpected values.
break;
}
}
private void TextBoxDataBinding(Object sender, EventArgs e)
{
TextBox myTextBox = (TextBox)sender;
GridViewRow row = (GridViewRow)myTextBox.NamingContainer;
myTextBox.Text = System.Web.UI.DataBinder.Eval(row.DataItem, columnName).ToString();
}
}
void Page_Load(Object sender, EventArgs e)
{
// Create the field columns when the page is first loaded.
if (!IsPostBack)
{
TemplateField tField;
// Create the dynamic templates
tField = new TemplateField();
tField.ItemTemplate = new GridViewTextTemplate(DataControlRowType.DataRow, "parameter_description", "textParameterDesc");
tField.HeaderTemplate = new GridViewTextTemplate(DataControlRowType.Header, "说明", "textParameterDescHd");
// Add the dynamic templates field column to the GridView
GridView1.Columns.Add(tField);
}
}
</script>
<html>
<body>
<form id="Form1" runat="server">
<asp:gridview id="GridView1"
datasourceid="TestSqlDataSource"
autogeneratecolumns="False"
runat="server">
</asp:gridview>
<asp:sqldatasource id="TestSqlDataSource"
selectcommand="Select * From Parameter_Lines"
connectionstring="server=localhost;database=Test;integrated security=SSPI"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
原帖地址:http://www.cnblogs.com/Fortunelee/articles/505044.html
相关推荐
ASP.NET中GridView控件TemplateField高级用法详解
GridView 是 ASP.NET Web Forms 中用于展示结构化数据的核心控件之一,具备强大的数据绑定能力与交互功能。它不仅可以展示来自数据库、集合或对象的数据,还支持内置的分页、排序、编辑、删除等操作,极大地提升了数据展示与管理的开发效率。在 GridView 中,数据通过列(Column)来组织展示,常见的列类型包括 BoundField、TemplateField、ButtonField 等。
C#中GridView动态添加列的实现方法
本文实例讲述了C#中GridView动态添加列的实现方法。分享给大家供大家参考。具体如下: protected void Page_Load(object sender, EventArgs e) { TemplateField mycustomField = new TemplateField(); //创建列实例 mycustomField.ShowHeader = true; // 设置属性 LinkButton lb = new LinkButton(); lb.Text = Delete; mycustomField.HeaderTemplate =
GridView动态添加模板列
GridView动态添加模板列,有设置,和代码,是学习GridView的好东西。
templatefield 动态_[ASP.NET][GridView] GridView 动态产生 TemplateField 字段
[ASP.NET][GridView] GridView 动态产生 TemplateField 字段最近有个需求,想尝试使用这样的方式,这就来笔记一下。我们依然选用 北风数据库参考:http://msdn.microsoft.com/zh-tw/library/system.web.ui.webcontrols.templatefield.templatefield%28VS.80%29.aspx...
templatefield 动态_动态添加TemplateField列
GridViewTextTemplate类:using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI.WebControls;/// ///GridViewTextTemplate 的摘要说明/// public class GridViewTextTem...
GridView控件数据验证
介绍ASP.NET 2.0 中的GridView控件可以让我们很快地修改和删除记录,但是现实中绝大部分情况是要求我们在对记录进行修改和删除时予以验证,有一些方法可以实现这个问题,本文将对这个问题从以下3个方面进行探索一下。 Client side validation using validation controls Server side validati
templatefield 动态_将 TemplateField 字段列动态添加到 GridView 控件 | 学步园
Page language="C#"%>runat="server">// Create adynamic template columnpublicclassGridViewTextTemplate :System.Web.UI.ITemplate{privateDataControlRowTypetemplateType;privatestringcolumnName;privatest...
templatefield 动态_将 TemplateField 字段列动态添加到 GridView 控件
// Create a dynamic template columnpublic class GridViewTextTemplate : System.Web.UI.ITemplate{private DataControlRowType templateType;private string columnName;private string cId;public GridViewTextT...
GridView动态创建模板列templateField
找了很多资料,对动态模板列的创建有一定的了解,集合了下网上的一些精华。继承ITemplate类,动态创建textbox,checkbox,dropdownlist等,并动态绑定数据public class GridViewItemTemplate:ITemplate { private string obj; //控件对象的字符串,以此来判断具体创建哪个控件
GridView动态生成列
/// /// gridview模板类重写 /// public class GvPlanTemplate : ITemplate { /// /// 构造函数 /// public GvPlanTemplate() { } /// /// 模板添加子控件 /// public void Insta
GridView动态添加列
GridView动态添加列 public class MyTemplate:ITemplate { private string strColumnName; private DataControlRowType dcrtColumnType; public MyTemplate(strin...
templatefield 动态_动态添加 GridView 的模板列
声明式数据绑定中介绍了 GridView 的模板,但这种模板都是在 .aspx 中定义好了的,如何利用程序动态制定模板呢?其实不复杂,首先要创建一个字段类。public class MyTemplate : System.Web.UI.ITemplate{public MyTemplate(){}public void InstantiateIn(System.Web.UI.Control con...
templatefield 动态_GridView动态创建templateField | 学步园
今天有些时间,把用cs代码添加编辑列及动态添加编辑模板作了整理。动态添加编辑列比较简单,即TemplateField tf=new TemplateField(),但难得是 往编辑列里动态添加编辑模版项 ItemTemplate。那天上网找了好多资料,才找到解决方法。先创建一个类继承模板接口 ITemplate,用构造函数实现返回Template类。废话不多说了,现给出例子,看了就清楚了。例子延续...
gridview 动态加载模板列
#region 自定义模板列类 public class GridViewTemplate : ITemplate { private string proName; public GridViewTemplate() { } public string ProName//要绑定的数据源字段名称
ASP.NET中GridView动态添加下拉框
ASP.NET中GridView动态添加下拉框//设计页GridView控件中动态添加下拉框列
GridView动态创建templateField
<br /> 今天有些时间,把用cs代码添加编辑列及动态添加编辑模板作了整理。动态添加编辑列比较简单,即 <br />TemplateField tf=new TemplateField(),但难得是 往编辑列里动态添加编辑模版项 ItemTemplate。那天上网找了好多资料,才找到解决方法。先创建一个类继承模板接口 ITemplate,用构造函数实现返回Template类。废话不多说了,现给出例子,看了就清楚了。例子延续前面的动态绑定字段列的。<br />创建Template 的类 cs:GVItemT
1074




被折叠的 条评论
为什么被折叠?



