更改放置在GridTemplateColumn中的Image的ImageURL

时间:2012-06-14 14:40:06

标签: c# asp.net image gridview telerik

我正在尝试在gridview中显示图像,但是,gridview中显示的列都是动态显示的(未生成)。

我已经查看了这篇文章,但它似乎不适用于我的情况: How to access ImageUrl property of the Image Control placed inside a ItemTemplate in the GridView

我所拥有的是包含这些列的gridview:

    <Columns> 
        <telerik:GridBoundColumn  HeaderText="Custom1"   HeaderStyle-VerticalAlign="Bottom" DataField="Custom1" SortExpression="Custom1"/>
        <telerik:GridDateTimeColumn HeaderText="Custom2"   HeaderStyle-VerticalAlign="Bottom" DataField="Custom2" SortExpression="Custom2"/>
        <telerik:GridCheckBoxColumn  HeaderText="Custom3"   HeaderStyle-VerticalAlign="Bottom" DataField="Custom3" SortExpression="Custom3"/>
        <telerik:GridTemplateColumn HeaderText="Custom4" HeaderStyle-VerticalAlign="Bottom" DataField="Custom4"><itemtemplate><asp:Image runat="server" ID="Custom4_pic"/></itemtemplate></telerik:GridTemplateColumn>

重复四列,更改Custom#值,直到达到48。

然后我继续隐藏所有不必要的列,只显示我需要的列。(这是因为用户可以定义要显示的列的顺序,并且硬编码全部是我可以快速实现它的最简单方法)。

所有内容都正确显示,但是,我似乎无法想象如何为图像控件设置ImageURL。对于其余的列,我就是这样做的:

dt = new DataTable();
// Do some stuff...
if (aProperty.WebControlType == EntityPropertyWebControlType.CheckboxYN || aProperty.WebControlType == EntityPropertyWebControlType.OptionYN)
{
    dt.Columns.Add("Custom" + ((i * 4) - 1).ToString(), typeof(bool));
    break;
}
else if (aProperty.WebControlType == EntityPropertyWebControlType.Date)
{
    dt.Columns.Add("Custom" + ((i * 4) - 2).ToString(), typeof(DateTime));
    break;
}
else if (aProperty.WebControlType == EntityPropertyWebControlType.Image)
{
    // Not sure what to do here...
    break;
}
else
{
    dt.Columns.Add("Custom" + ((i*4) - 3).ToString(), typeof(string));
    break;
}

// Do more stuff...

// Based on type I will set the values to equal something.. so for example
dr["Custom" + aProperty.LabelColumnNo.ToString()] = "Text" or bool or DateTime
// However I'm not sure what to do for Image...

// and then I return the datatable to be binded to the gridview

任何提示都会很棒,谢谢!

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题。

我用GridImageColumns替换了模板列,然后在我的代码隐藏中,我将Columns添加到string类型的数据表中,然后我使用了一个图像处理程序,并将字符串设置为图像处理程序页面。

相关问题