后台点击事件的时候,再把这个模板列的Visible=true就可以了
gridView.Columns[i].Visible = true
可以实现,但是我告诉你。动态生成模板列是非常难取到数据的。我试过,就是取不到数据。这是经过我改造后的样品,自己研究下。
using System
using System.Data
using System.Configuration
using System.Linq
using System.Web
using System.Web.Security
using System.Web.UI
using System.Web.UI.HtmlControls
using System.Web.UI.WebControls
using System.Web.UI.WebControls.WebParts
using System.Xml.Linq
/// <summary>
///动态生成Template
/// </summary>
public class MyTemplate:ITemplate
{
private string strColumnName
private DataControlRowType dcrtColumnType
public MyTemplate()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
/**//// <summary>
/// 动态添加模版列
/// </summary>
/// <param name="strColumnName">列名</param>
/// <param name="dcrtColumnType">列的类型</param>
public MyTemplate(string strColumnName, DataControlRowType dcrtColumnType)
{
this.strColumnName = strColumnName
this.dcrtColumnType = dcrtColumnType
}
static int count1 = 0
public void InstantiateIn(Control ctlContainer)
{
switch (dcrtColumnType)
{
case DataControlRowType.Header: //列标题
Literal ltr = new Literal()
ltr.Text = strColumnName
ctlContainer.Controls.Add(ltr)
break
case DataControlRowType.DataRow: //模版列内容——加载TextBox
TextBox tb = new TextBox()
tb.ID ="TextBox"+count1.ToString()
tb.EnableViewState = true
tb.Width =35
ctlContainer.Controls.Add(tb)
//Label lbl = new Label()
//lbl.ID = "Lable" + count1.ToString()
//ctlContainer.Controls.Add(tb)
count1++
break
}
}
/// <summary>
/// 绑定生成GridView
/// </summary>
/// <param name="gdv">要绑定的GridView</param>
/// <param name="dtblDataSource">GridView的数据源</param>
/// <param name="strDataKey">GridView的DataKeyNames</param>
public static void GridViewBind(GridView gdv, DataSet dtblDataSource)
{
//gdv.Columns.Clear()
gdv.AutoGenerateColumns = false
gdv.DataSource = dtblDataSource
int count=dtblDataSource.Tables[0].Columns.Count
//gdv.DataKeyNames = new string[]{ strDataKey }
TemplateField[] tfColumn = new TemplateField[count]
for (int i = 0i <counti++)
{
tfColumn[i] = new TemplateField()
tfColumn[i].ItemTemplate = new MyTemplate("", DataControlRowType.DataRow)
tfColumn[i].HeaderTemplate = new MyTemplate(dtblDataSource.Tables[0].Columns[i].ToString(), DataControlRowType.Header)
tfColumn[i].EditItemTemplate = new MyTemplate("", DataControlRowType.DataRow)
gdv.Columns.Add(tfColumn[i])
//if (i==count - 1)
//{
//TemplateField t = new TemplateField()
//t.ItemTemplate = new MyTemplate("", DataControlRowType.DataRow)
//t.HeaderTemplate = new MyTemplate("保存", DataControlRowType.Header)
//gdv.Columns.Add(t)
//}
}
gdv.DataBind()
}
}
数据库中保存图片的路径(../image/1.jpg)赋值给图片模板中的图片控件。在保存图片路径的时候就将图片存放的文件夹及其图片名称都保存,这样再提取图片路径的时候就可以跟一般控件赋值一样了。欢迎分享,转载请注明来源:内存溢出
评论列表(0条)