I have a image control inside gridview along with other attributes. I want to know how can I set the image url property or How can I assign certain static Images based on attribute value say customer name? I want to do this since I am not storing images in my database.Please guide me how can I do this? Do I need to use RowDataBound
property of the gridview?
I wanted to try something like this-
dt = g1.return_dt("select cust_name from tbl_details");
if(dt.Rows.Equals("XYZ"))
{
Set Image Url
}
else if(dt.Rows.Equals("XXX"))
{
Set Image Url
}
Table: "tblA"
id int identity
name nvarchar(50)
ASPX:
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView><br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [tblA]">
</asp:SqlDataSource>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
//fill gridview
if( ! IsPostBack )
{
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
string name = e.Row.Cells[2].Text;
Image img = (Image)e.Row.FindControl("Image1");
if (img==null)
{
return;
}
if(name == "XYZ")
{
img.ImageUrl = "~/Images/a.jpg";
}
else if(name == "XPS")
{
img.ImageUrl = "~/Images/b.jpg";
}
else
{
//default static image
img.ImageUrl = "~/Images/c.jpg";
}
}