gridview,如何在代码后面建立一个图片网址?

时间:2011-02-10 00:19:44

标签: c# visual-studio-2008 .net-3.5 gridview

我有一个包含图像字段的gridview控件。但是,图像URL是从我返回的数据集中的几个字段构建的。你如何将这些字段连接在一起,以及我在哪一点上这样做以便我可以传递该图像url在我的gridview中执行我的图像字段?我猜它在RowDataBound上,但我不知道如何访问我的数据集中的每一行?

感谢。

3 个答案:

答案 0 :(得分:0)

在.NET 4中,它将是这样的:

if (e.Row.RowType == DataControlRowType.DataRow)  
{  
    string1 = e.Row.Cells[i].Text;
}

i是您要引用的特定列的索引(例如Cells[0]是列1)。

我不确定它在3.5中是否有所不同,但尝试一下。

答案 1 :(得分:0)

您可以使用RowDataBound事件来设置单元格值。

void YourGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {

    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // You can set your cell value in this
      e.Row.Cells[index] // use this to set the value in the cell 
    }

  }

答案 2 :(得分:0)

如果没有代码示例,我不确定你要连接的是什么,但是我会在绑定gridview之前执行连接并将其存储在类的私有成员中,以便稍后可以在 RowDataBound 事件。

您可以使用行数据绑定事件来查找该行中的控件并设置其ImageUrl属性。

private string m_ConcatUrl;

protected void gridView_RowDataBound(Object sender, GridViewRowEventArgs args)
{
    if(args.Row.RowType == DataControlRowType.DataRow)
    {
        Image imgCtrl = (Image) args.Row.FindControl("imgCtrl");
        imgCtrl.ImageUrl = m_ConcatUrl;
    }
}