Asp.net 在Asp Datagrid中查看图片的Itemtemplate(在代码中)
我正在为sharepoint编写一个Web部件,因此我必须有问题地生成一个Datagrid 情况是,我得到一个数据视图,生成GRI并绑定数据。 一列应该显示一个图像,因此我必须使用item template生成一个模板列 代码如下所示:Asp.net 在Asp Datagrid中查看图片的Itemtemplate(在代码中),asp.net,datagrid,itemtemplate,Asp.net,Datagrid,Itemtemplate,我正在为sharepoint编写一个Web部件,因此我必须有问题地生成一个Datagrid 情况是,我得到一个数据视图,生成GRI并绑定数据。 一列应该显示一个图像,因此我必须使用item template生成一个模板列 代码如下所示: //Instantiate the DataGrid, and set the DataSource _grdResults = new DataGrid(); _grdResults.AutoGenerateColumns = false; _grdResul
//Instantiate the DataGrid, and set the DataSource
_grdResults = new DataGrid();
_grdResults.AutoGenerateColumns = false;
_grdResults.DataSource = view;
TemplateColumn colPic = new TemplateColumn();
colPic.HeaderText = "Image";
我找到了许多asp创建项目模板的示例,但是如何在代码中构造一个并将其ImageUrl绑定到Dataview的“imgURL”呢
谢谢你的建议
Ren您需要创建一个实现ITemplate接口的类
public class TemplateImplementation : ITemplate
{
public void InstantiateIn(Control container)
{
Image image = new Image();
image.DataBinding += Image_DataBinding;
container.Controls.Add(image);
}
void Image_DataBinding(object sender, EventArgs e)
{
Image image = (Image)sender;
object dataItem = DataBinder.GetDataItem(image.NamingContainer);
// If the url is a property of the data item, you can use this syntax
//image.ImageUrl = (string)DataBinder.Eval(dataItem, "ThePropertyName");
// If the url is the data item then you can use this syntax
image.ImageUrl = (string)dataItem;
}
}
然后将ItemTemplate设置为此类的实例
colPic.ItemTemplate = new TemplateImplementation();
您需要创建一个实现ITemplate接口的类
public class TemplateImplementation : ITemplate
{
public void InstantiateIn(Control container)
{
Image image = new Image();
image.DataBinding += Image_DataBinding;
container.Controls.Add(image);
}
void Image_DataBinding(object sender, EventArgs e)
{
Image image = (Image)sender;
object dataItem = DataBinder.GetDataItem(image.NamingContainer);
// If the url is a property of the data item, you can use this syntax
//image.ImageUrl = (string)DataBinder.Eval(dataItem, "ThePropertyName");
// If the url is the data item then you can use this syntax
image.ImageUrl = (string)dataItem;
}
}
然后将ItemTemplate设置为此类的实例
colPic.ItemTemplate = new TemplateImplementation();
如果必须执行多列操作,则可能需要创建一个GenericTemplate类,该类接受实例化EIN和数据绑定方法的委托。如果必须执行多列操作,则可能需要创建一个GenericTemplate类,该类接受实例化EIN和数据绑定方法的委托。