2011-04-22 80 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