gridview模板列的问题
发布网友
发布时间:2023-09-14 02:20
我来回答
共2个回答
热心网友
时间:2024-11-25 14:42
App_Code/DataGridTemplate.cs:
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
/// <summary>
///DataGridTemplate 的摘要说明
/// </summary>
public class DataGridTemplate : ITemplate
{
ListItemType templateType;
string columnName;
public DataGridTemplate(ListItemType type , string cname)
{
templateType = type;
columnName = cname;
}
public void InstantiateIn(System.Web.UI.Control container)
{
Literal lc = new Literal();
switch(templateType)
{
case ListItemType.Header:
lc.Text = "<B>" + columnName + "</B>";
container.Controls.Add(lc);
break;
case ListItemType.Item:
if(columnName == "myCol")
{
Button btn = new Button();
btn.Text = "myBTn";
Image img = new Image();
img.AlternateText = "myImg";
HtmlGenericControl hgc = new HtmlGenericControl();
hgc.TagName = "div";
hgc.InnerHtml = "myDiv";
container.Controls.Add(btn);
container.Controls.Add(img);
container.Controls.Add(hgc);
}
break;
}
}
}
Default.aspx.cs:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GridView gv = new GridView();
gv.AutoGenerateColumns = false;
TemplateField tf1 = new TemplateField();
tf1.HeaderTemplate = new
DataGridTemplate(ListItemType.Header , "自定义模板列");
tf1.ItemTemplate = new
DataGridTemplate(ListItemType.Item , "myCol");
gv.Columns.Add(tf1);
DataSet ds= new DataSet();
//读取数据
gv.DataSource = ds;
gv.DataBind();
form1.Controls.Add(gv);
}
}
热心网友
时间:2024-11-25 14:43
什么意思?自动生成是怎么个意思???