GridView动态添加模板列
来源:互联网 发布:质量控制软件 编辑:程序博客网 时间:2024/04/29 00:57
1、首先添加一个模板类
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace NSDPWeb.DbAccess.Introduction{ //动态添加模版类 public class GridViewTemplate : ITemplate { public delegate void EventHandler(object sender, EventArgs e); public event EventHandler eh; private DataControlRowType templateType; private string columnName; private string controlID; public GridViewTemplate(DataControlRowType type, string colname) { templateType = type; columnName = colname; } public GridViewTemplate(DataControlRowType type, string controlID, string colname) { templateType = type; this.controlID = controlID; columnName = colname; } public void InstantiateIn(System.Web.UI.Control container) { switch (templateType) { case DataControlRowType.Header: Literal lc = new Literal(); lc.Text = columnName; container.Controls.Add(lc); break; case DataControlRowType.DataRow: LinkButton lbtn = new LinkButton(); lbtn.ID = this.controlID; if (eh != null) { lbtn.Click += new System.EventHandler(eh); } lbtn.DataBinding += new System.EventHandler(lbtn_DataBinding); container.Controls.Add(lbtn); break; default: break; } } void lbtn_DataBinding(object sender, EventArgs e) { LinkButton lbtn = sender as LinkButton; if (lbtn != null) { GridViewRow container = lbtn.NamingContainer as GridViewRow; if (container != null) { object dataValue = DataBinder.Eval(container.DataItem, columnName); if (dataValue != DBNull.Value) { lbtn.Text = dataValue.ToString(); } } } } } }
2、前台
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
</asp:GridView>
3、后台
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NSDPWeb.DbAccess.Introduction;
using DataAccess;
namespace NSDPWeb.Pages.Introduction
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable table =BaseDb.RunProcedureForQuery("IntroSelectAll", null).Tables[0];
GridView1.DataSource = table;
GridView1.DataBind();
}
protected override void OnInit(EventArgs e)
{
TemplateField customField = new TemplateField();
customField.ShowHeader = true;
customField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "动态添加列");
GridViewTemplate gvt = new GridViewTemplate(DataControlRowType.DataRow, "lbtn", "姓名");
gvt.eh += new GridViewTemplate.EventHandler(lbtn_Click);
customField.ItemTemplate = gvt;
GridView1.Columns.Add(customField);
base.OnInit(e);
}
public void lbtn_Click(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(GetType(), "test", "alert('ok');", true);
//Response.Redirect(String.Format("~/Pages/Introduction/IntroStudentDetail.aspx?numberid={0}&category={1}", Eval("编号"), Eval("类别")));
}
}
}
- GridView动态添加模板列
- GridView动态添加模板列
- GridView动态添加模板列
- GridView动态添加模板列
- GridView动态添加模板列(数据绑定)
- GridView 动态添加绑定列和模板列
- GridView 动态添加列
- gridview动态添加列
- gridView动态添加列
- GridView动态添加列
- ASP.NET 2.0 中动态添加 GridView 模板列的...
- ASP.NET 2.0 中动态添加 GridView 模板列
- ASP.NET2.0中动态添加GridView模板列
- GridView动态添加模板列(VB.NET版)
- GridView动态添加模板列并自动绑定数据
- 动态添加GridView模板
- Gridview后台添加模板列
- GridView后台添加模板列
- django+python+eclipse 入门hello,打印时间
- python 获取网络时间及修改本地时间
- 链接分析算法之:主题敏感PageRank
- NiosII软处理器快速入门- 10分钟学会NiosII
- jQuery 简单实例:演示如何隐藏部分文本
- GridView动态添加模板列
- IntelliJ IDEA常用设置
- 许多GDI+ 方法都是重载的
- 七夕夜、离别,涵情经河岸
- xml解析方法
- ASP.NET Ajax复习文献
- ubuntu解决打开windows记事本.txt文件乱码的方法
- jasperReport Exporter常用参数说明
- C++构造函数是否可以定义为private