2011-04-22 83 views
1

我想知道如何将图像添加到基于XML文档中的imageurl的网格视图。到目前为止,我已经......LINQ to XML和GridView.ImageField

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(); 

的正常工作,以显示拇指领域的网址,但而不是显示该如何更改它的像场?

+0

显示您的GridView标记 – abatishchev 2011-04-22 09:28:42

回答

0

标记:

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

代码隐藏:

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(); 

你有什么问题?

+0

是的,我可以做到这一点是好的,但只希望gridview显示图像,而不是它当前所做的filepatth – Kev 2011-04-22 09:35:51

+0

@Kev:看起来像图像的路径如何?它应该与包含GridView的控件相关。或服务器相关的,例如'〜/ Images/foo.gif' – abatishchev 2011-04-22 09:38:42

+0

gridview现在有两列。一个图像的URL,并在它旁边绑定它的图像 – Kev 2011-04-22 09:44:01