1

i am wanting to know how to add an image to a gridview based on the imageurl in a xml document. so far i have...

XDocument xmlDoc = XDocument.Load(Server.MapPath("XMLFile.xml"));

var q = from c in xmlDoc.Descendants("Images")
        where c.Element("PropertyId").Value.ToString() == DropDownList1.SelectedValue.ToString()
        select new
        {
            Id = c.Element("PropertyId").Value,
            Thumb = c.Element("ThumbUrl").Value                
        };
GridView1.DataSource = q;
GridView1.DataBind();

which works fine to show the url in the thumb field but instead of showing this how do i change it an image field?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Kev
  • 41
  • 1
  • 1
  • 3

1 Answers1

0

Markup:

<asp:GridView runat="server">
    <Columns>
        <ImageField DataImageUrlField="PhotoPath" />
    </Columns>
</<asp:GridView>

Code-behind:

string selectedValue =  DropDownList1.SelectedValue.ToString(); // cache it!
var q = from c in xmlDoc.Descendants("Images")
        where c.Element("PropertyId").Value.ToString() == selectedValue 
        select new
        {
            PhotoPath = c.Element("PhotoPath").Value         
        };

GridView1.DataSource = q;
GridView1.DataBind();

What is your problem?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • yeah i can do that which is fine but only want the gridview to show the image not the filepatth which it currently does – Kev Apr 22 '11 at 09:35
  • @Kev: How does look like images' path? It should be relative to control containing the GridView. Or server-relative, e.g. `~/Images/foo.gif` – abatishchev Apr 22 '11 at 09:38
  • the gridview now has two columns. one the url of the image and next to it the image that is bound to it – Kev Apr 22 '11 at 09:44
  • @Kev: Is a path to image relative, absolute, or what? – abatishchev Apr 22 '11 at 09:45