Click here to Skip to main content
15,882,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I need to load images in the gridview dynamically, I use the below code through which I'm unable to load the images in the gridview. Kindly let me know which went wrong in this code.

Please find the code below:

The code in Default.aspx file

XML
<form id="form1" runat="server">

<div>
<asp:GridView ID="gvImages" runat="server" AutoGenerateColumns="False"
HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="white">
<Columns>
<asp:BoundField HeaderText = "Image Name" DataField="imagename" />
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("ImageID") %>' Height="150px" Width="150px"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>


The code in Default.aspx.cs file

C#
protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            BindGridData();
        }
    }
   
    /// <summary>
    /// function is used to bind gridview
    /// </summary>
    public void BindGridData()
    {
        
        DataTable dt = new DataTable();
       
        dt.Columns.Add("imagename");
        dt.Columns.Add("ImageID");


        DataRow dr = dt.NewRow();
        dr["imagename"] = "Image1";
        dr["ImageID"] = @"D:\Mathi_Working\01816_Backup\Mathi\Images\9.gif";
        
        dt.Rows.Add(dr);


        DataRow dr1 = dt.NewRow();
        dr1["imagename"] = "Image2";
        dr1["ImageID"] = @"D:\Mathi_Working\01816_Backup\Mathi\Images\HereSmiley.JPG";
        dt.NewRow();
        dt.Rows.Add(dr1);

        gvImages.DataSource = dt;
        gvImages.DataBind();
        gvImages.Attributes.Add("bordercolor", "black");
    }


Kindly let me know where I'm making mistake.

Thanks & Regards,
Mathi.
Posted

Do not give Image path like this is the absolute path and will not work.

dr1["ImageID"] = @"D:\Mathi_Working\01816_Backup\Mathi\Images\HereSmiley.JPG";


This should be a like...

dr1["ImageID"] = @"http://localhost:53734/Mathi/Images/HereSmiley.JPG";
 
Share this answer
 
Comments
Mathi2code 30-Jul-13 8:55am    
tried like this dr1["ImageID"] = @"http://localhost:1311/Images/HereSmiley.JPG"; the image is not loading...
Previously nothing was displaying now I'm getting a default image icon which has been broken
idenizeni 30-Jul-13 18:52pm    
Review the HTML source to ensure you are referencing the correct path to your images directory. Right click the default image in your browser and check the path... is it the correct path to your image?
Mathi2code 30-Jul-13 23:58pm    
When I right clicked and selected Copy Image Url I'm getting this http://localhost:1311/Images/9.gif
and in my system the file is present in the folder path D:\Website1\Images\9.gif
idenizeni 31-Jul-13 16:12pm    
If D:\Website1\ is your site's root then the image's path looks correct to me. Are you certain the 9.gif file exists in the images directory?
Mathi2code 1-Aug-13 0:06am    
It is there in the physical path but when I try to right click the default image icon and open in new tab am getting an error saying file not found 404 issue. I'm not even hosting it to IIS I've just create the website appln. and clicked on debug button.
You can do the same in Grid View RowBound event also... find the attached code snippet

C#
protected void grd_listOutsourceRequest_RowDataBound(object sender, GridViewRowEventArgs e)
       {


           if (e.Row.RowType == DataControlRowType.DataRow)
           {
               Image img = new Image();

             img.ImageUrl = "../Images/Blue.png";
                       img.Visible = true;
                       e.Row.Cells[6].Text = "Open";
                       e.Row.Cells[6].Controls.Add(img);


           }


       }
 
Share this answer
 
Comments
Mathi2code 30-Jul-13 8:52am    
tried giving like this
dr["ImageID"] = "../Images/9.gif";
still not working
but Previously nothing was displaying now I'm getting a default image icon which has been broken
The below code solved my problem

C#
string rootImgPath = ResolveUrl("~/Mathi/Images/");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900