Search code examples
c#visual-studio-2008.net-3.5gridview

gridview, how to build a image url in code behind?


I have a gridview control which contains an image field. However, the image url is built from several fields in my returned dataset. How do you concatenate those fields together, and at which point do I do this so that I can pass that image url do my imagefield in my gridview? I am guessing it's on the RowDataBound but I don't know how to access each of the rows in my dataset?

Thanks.


Solution

  • I'm not sure what you are trying to concatenate without a code example, however I would perform the concatenation before binding the gridview and store it in a private member on the class so that you can access it later in the RowDataBound event.

    You can use the row data bound event to find the control within that row and set its ImageUrl property.

    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;
        }
    }