急!!!怎么纶GridView动态添加模板列? 我需要给GridView动态添加一个模板列,该模板列中包含一个LinkButton,LinkButton的text绑定HOSPITAL急啊,在线等~~~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1.创建实现System.Web.UI.ITemplate 接口的新类. public class MyTemplate : ITemplate { private string proName; public MyTemplate() { // // TODO: 在此处添加构造函数逻辑 // } public string ProName//要绑定的数据源字段名称 { set { proName = value; } get { return proName; } } public void InstantiateIn(Control container)//关键实现这个方法 { HtmlImage hi = new HtmlImage(); hi.Src = ""; hi.Alt = ""; hi.DataBinding += new EventHandler(hi_DataBinding);//创建数据绑定事件 container.Controls.Add(hi); } void hi_DataBinding(object sender, EventArgs e) { HtmlImage hi = (HtmlImage)sender; GridViewRow container = (GridViewRow)hi.NamingContainer; //关键位置 //使用DataBinder.Eval绑定数据 //ProName,MyTemplate的属性.在创建MyTemplate实例时,为此属性赋值(数据源字段) hi.Attributes.Add("onclick", "alert('" + DataBinder.Eval(container.DataItem, ProName).ToString() + "');"); } } 2.*.aspx页面后台cs代码 if (!this.IsPostBack) { DataSet ds = null; BLL.model_task bll = new BLL.model_task(); ds = bll.GetList(string.Empty); TemplateField tf = new TemplateField(); tf.HeaderText = "自定义模板列"; MyTemplate mt = new MyTemplate(); mt.ProName = "m_task_name";//数据源字段 tf.ItemTemplate = mt; this.GridView1.Columns.Add(tf); this.GridView1.DataSource = ds; this.GridView1.DataBind(); } 参考下,网上很多http://hi.baidu.com/sharenet/blog/item/df14b51c9a74078e86d6b65f.html http://topic.csdn.net/u/20071113/23/A85BB167-DE36-4775-8030-4692B8E23C67.htmlhttp://www.cnblogs.com/feb9903/archive/2007/04/13/712462.htmlhttp://www.cnblogs.com/lmarsy/archive/2006/11/18/564634.htmlhttp://www.pin5i.com/showtopic-7134.htmlhttp://dotnet.aspx.cc/article/897cb049-ccff-4fe7-b990-df974493a0b1/read.aspx 如何使用VS 2005 asp.net技术制作项目计划甘特图? .net如何在发送的确认邮件里知道是哪个用户点击了? 求一正则表达式 dropdownlist出现的问题(急!!在线等) 关于下载页面的问题 在这个代码里 我怎么截取某个字段里的一段字符啊 utf-8(gb2312)转big5,请教 asp.net 如何操作 客户端 的 foxpro数据库 大家都来看看 calendar控件,选中一天,改变这天所在cell的属性,100分求解!--解决马上结贴 excel导出功能 sql distinct 问题 删除文本框内容,怎么触发???
public class MyTemplate : ITemplate
{
private string proName;
public MyTemplate()
{
//
// TODO: 在此处添加构造函数逻辑
//
} public string ProName//要绑定的数据源字段名称
{
set { proName = value; }
get { return proName; }
} public void InstantiateIn(Control container)//关键实现这个方法
{
HtmlImage hi = new HtmlImage();
hi.Src = "";
hi.Alt = "";
hi.DataBinding += new EventHandler(hi_DataBinding);//创建数据绑定事件
container.Controls.Add(hi);
} void hi_DataBinding(object sender, EventArgs e)
{
HtmlImage hi = (HtmlImage)sender;
GridViewRow container = (GridViewRow)hi.NamingContainer;
//关键位置
//使用DataBinder.Eval绑定数据
//ProName,MyTemplate的属性.在创建MyTemplate实例时,为此属性赋值(数据源字段)
hi.Attributes.Add("onclick", "alert('" + DataBinder.Eval(container.DataItem, ProName).ToString() + "');");
}
}
2.*.aspx页面后台cs代码
if (!this.IsPostBack)
{
DataSet ds = null;
BLL.model_task bll = new BLL.model_task();
ds = bll.GetList(string.Empty); TemplateField tf = new TemplateField();
tf.HeaderText = "自定义模板列";
MyTemplate mt = new MyTemplate();
mt.ProName = "m_task_name";//数据源字段
tf.ItemTemplate = mt;
this.GridView1.Columns.Add(tf);
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
http://hi.baidu.com/sharenet/blog/item/df14b51c9a74078e86d6b65f.html
http://www.cnblogs.com/feb9903/archive/2007/04/13/712462.html
http://www.cnblogs.com/lmarsy/archive/2006/11/18/564634.html
http://www.pin5i.com/showtopic-7134.html
http://dotnet.aspx.cc/article/897cb049-ccff-4fe7-b990-df974493a0b1/read.aspx