Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在代码中更改GridView中的图像DataImageUrlFormatString值?_C#_Asp.net_Vb.net_Gridview - Fatal编程技术网

C# 如何在代码中更改GridView中的图像DataImageUrlFormatString值?

C# 如何在代码中更改GridView中的图像DataImageUrlFormatString值?,c#,asp.net,vb.net,gridview,C#,Asp.net,Vb.net,Gridview,我有一个gridview,coulmn是一个图像列。 如何更改代码隐藏中的DataImageUrlFormatString值 我试过这样做,但不起作用 ((ImageField)(GridView2.Rows[0].Cells[0].FindControl("ID"))).DataImageUrlFormatString = "~/createthumb.ashx?gu=/pics/gmustang06_2.jpg"; 试试这个: ((System.Web.UI.WebControls

我有一个gridview,coulmn是一个图像列。 如何更改代码隐藏中的DataImageUrlFormatString值

我试过这样做,但不起作用

((ImageField)(GridView2.Rows[0].Cells[0].FindControl("ID"))).DataImageUrlFormatString 

 = "~/createthumb.ashx?gu=/pics/gmustang06_2.jpg";
试试这个:

 ((System.Web.UI.WebControls.Image)(GridView2.Rows[0].Cells[0].Controls[0])).ImageUrl = "~/createthumb.ashx?gu=/pics/gmustang06_2.jpg";
编辑:

您可以使用声明性语法将路径的URL设置为将在图像控件中显示的图像:

<asp:ImageField DataImageUrlField="id" DataImageUrlFormatString="img{0}.jpg"></asp:ImageField>

谢谢,它确实有效,但是如果我想更改每个单元格的图像字段,而不仅仅是单元格[0]上的图像字段,我必须做什么,在哪里做。就像我在GridView中有一个图像字段和一个从DB中获取的ID字段一样。如果ID为1,则我要显示Image1.jpg;如果ID为2,则我要显示Image2.jpg,依此类推。是否必须在我的RowDataBound事件中执行此操作?
protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Image img = e.Row.Cells[0].Controls[0] as Image;
        img.ImageUrl = "img" + DataBinder.Eval(e.Row.DataItem, "id") + ".jpg";
    }      
}