Etienne
Etienne

Reputation: 7201

How to change my Image DataImageUrlFormatString Value inside my GridView in Code?

I have a gridview and the one coulmn is a image column. How would i change the DataImageUrlFormatString value in my code behind?

i tried doing this but it does not work.......

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

 = "~/createthumb.ashx?gu=/pics/gmustang06_2.jpg";

Upvotes: 2

Views: 9047

Answers (1)

Phaedrus
Phaedrus

Reputation: 8421

Try this:

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

EDIT:

You can set the URL of the path to an image that will be displayed in the image control with declarative syntax:

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

or in the Code Behind by handling the OnRowDataBound event of the GridView control:

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

Upvotes: 4

Related Questions