GridView 动态生成模版列
最近项目中有用到GridView 的列是动态生成的,并且是用模版列.查找了网上的方法.作法如下:
1. 写一个类.主要继承ITemplate
Code
public class GridViewTemplate : ITemplate
{
private DataControlRowType templateType; //区分列的类型
private string columnName; //名称
public GridViewTemplate(DataControlRowType type, string colname) //构造
{
templateType = type;
columnName = colname;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (templateType)
{
case DataControlRowType.Header: //列头
Literal lc = new Literal();
lc.Text = "<B>" + columnName + "</B>"; //列头名称
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow: //数据行
LinkButton lbnFileName = new LinkButton(); //添加LinkButton控件,正常显示
lbnFileName.ID = "lbn" + columnName;
lbnFileName.DataBinding += new EventHandler(lbnFileName_DataBinding);//数据绑定
lbnFileName.Click += new EventHandler(lbnFileName_Click);//Click 事件
container.Controls.Add(lbnFileName);
//Guid
Label lblGuid = new Label(); //添加lable ,隐藏
lblGuid.ID = "lblGuid" + columnName;
lblGuid.DataBinding += new EventHandler(lblGuid_DataBinding);
lblGuid.Visible = false;
container.Controls.Add(lblGuid);
//Type
Label lblType = new Label(); //添加lable ,隐藏
lblType.ID = "lblType" + columnName;
lblType.DataBinding += new EventHandler(lblType_DataBinding);
lblType.Visible = false;
container.Controls.Add(lblType);
break;
default:
break;
}
}
void lblType_DataBinding(object sender, EventArgs e) //Lable 的数据绑定
{
Label lbl = (Label)sender;
GridViewRow row = (GridViewRow)lbl.NamingContainer;
lbl.Text = DataBinder.Eval(row.DataItem, "lblType" +columnName).ToString();
}
void lblGuid_DataBinding(object sender, EventArgs e) //Lable 的数据绑定
{
Label lbl = (Label)sender;
GridViewRow row = (GridViewRow)lbl.NamingContainer;
lbl.Text = DataBinder.Eval(row.DataItem, "lblGuid" +columnName).ToString();
}
void lbnFileName_Click(object sender, EventArgs e) //LinkButton的Click 事件,这里主要是来下载文档
{
try
{
GridViewRow objGridViewRow = (GridViewRow)((LinkButton)sender).NamingContainer;
string strDQAManualFolder = HttpContext.Current.Server.MapPath(@"~\DOC\Manual");
string strFileName = ((LinkButton)objGridViewRow.FindControl("lbn" + columnName)).Text;
string strGuid = ((Label)objGridViewRow.FindControl("lblGuid" + columnName)).Text;
string strFileType = ((Label)objGridViewRow.FindControl("lblType" + columnName)).Text;
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF7) + "");
HttpContext.Current.Response.TransmitFile(strDQAManualFolder + "\\" + strGuid + '.' + strFileType);
}
catch (Exception ex)
{
//Response.Write(ex.ToString());
}
}
void lbnFileName_DataBinding(object sender, EventArgs e)//LinkButton 数据绑定
{
LinkButton lbn = (LinkButton)sender;
GridViewRow row = (GridViewRow)lbn.NamingContainer;
lbn.Text = DataBinder.Eval(row.DataItem, columnName).ToString();
}
}
public class GridViewTemplate : ITemplate
{
private DataControlRowType templateType; //区分列的类型
private string columnName; //名称
public GridViewTemplate(DataControlRowType type, string colname) //构造
{
templateType = type;
columnName = colname;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (templateType)
{
case DataControlRowType.Header: //列头
Literal lc = new Literal();
lc.Text = "<B>" + columnName + "</B>"; //列头名称
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow: //数据行
LinkButton lbnFileName = new LinkButton(); //添加LinkButton控件,正常显示
lbnFileName.ID = "lbn" + columnName;
lbnFileName.DataBinding += new EventHandler(lbnFileName_DataBinding);//数据绑定
lbnFileName.Click += new EventHandler(lbnFileName_Click);//Click 事件
container.Controls.Add(lbnFileName);
//Guid
Label lblGuid = new Label(); //添加lable ,隐藏
lblGuid.ID = "lblGuid" + columnName;
lblGuid.DataBinding += new EventHandler(lblGuid_DataBinding);
lblGuid.Visible = false;
container.Controls.Add(lblGuid);
//Type
Label lblType = new Label(); //添加lable ,隐藏
lblType.ID = "lblType" + columnName;
lblType.DataBinding += new EventHandler(lblType_DataBinding);
lblType.Visible = false;
container.Controls.Add(lblType);
break;
default:
break;
}
}
void lblType_DataBinding(object sender, EventArgs e) //Lable 的数据绑定
{
Label lbl = (Label)sender;
GridViewRow row = (GridViewRow)lbl.NamingContainer;
lbl.Text = DataBinder.Eval(row.DataItem, "lblType" +columnName).ToString();
}
void lblGuid_DataBinding(object sender, EventArgs e) //Lable 的数据绑定
{
Label lbl = (Label)sender;
GridViewRow row = (GridViewRow)lbl.NamingContainer;
lbl.Text = DataBinder.Eval(row.DataItem, "lblGuid" +columnName).ToString();
}
void lbnFileName_Click(object sender, EventArgs e) //LinkButton的Click 事件,这里主要是来下载文档
{
try
{
GridViewRow objGridViewRow = (GridViewRow)((LinkButton)sender).NamingContainer;
string strDQAManualFolder = HttpContext.Current.Server.MapPath(@"~\DOC\Manual");
string strFileName = ((LinkButton)objGridViewRow.FindControl("lbn" + columnName)).Text;
string strGuid = ((Label)objGridViewRow.FindControl("lblGuid" + columnName)).Text;
string strFileType = ((Label)objGridViewRow.FindControl("lblType" + columnName)).Text;
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF7) + "");
HttpContext.Current.Response.TransmitFile(strDQAManualFolder + "\\" + strGuid + '.' + strFileType);
}
catch (Exception ex)
{
//Response.Write(ex.ToString());
}
}
void lbnFileName_DataBinding(object sender, EventArgs e)//LinkButton 数据绑定
{
LinkButton lbn = (LinkButton)sender;
GridViewRow row = (GridViewRow)lbn.NamingContainer;
lbn.Text = DataBinder.Eval(row.DataItem, columnName).ToString();
}
}
2. GridView 模版列的添加
Code
1 TemplateField customField;
2 customField = new TemplateField();
3 customField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, listDesc);
4 customField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, column);
5 this.gdvChnManual.Columns.Add(customField);
6
7 //这里可以自由加列
8 this.gdvChnManual.DataSource = dt;
9 this.gdvChnManual.DataBind();
1 TemplateField customField;
2 customField = new TemplateField();
3 customField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, listDesc);
4 customField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, column);
5 this.gdvChnManual.Columns.Add(customField);
6
7 //这里可以自由加列
8 this.gdvChnManual.DataSource = dt;
9 this.gdvChnManual.DataBind();