如何使用LINQ在查询中显示图像?



我想在航空公司名称下面显示航空公司图像。我已经在XML文件中为airlineImage存储了路径。我该如何在使用LINQ的查询中显示图像?

下面是目前为止的代码:
var query = from f in XElement.Load(MapPath("flightdata2.xml")).Elements("flight")
                    select new
                    {                        
                        Airline = (string)f.Element("airline"),
                        DepartureAirportSymbol = (string)f.Element("departureAirportSymbol"),
                        DepartTime = (string)f.Element("departureTime"),
                        DestinationAirportSymbol = (string)f.Element("destinationAirportSymbol"),
                        ArrivalTime = (string)f.Element("arrivalTime"),
                        Stops = (int)f.Element("numberOfStops"),
                        Duration = (string)f.Element("duration"),
                        Cabin = (string)f.Element("class"),
                        Price = "$" + (Int32)f.Element("price")
                    };
    this.GridView1.DataSource = query;
    this.GridView1.DataBind();

试试这个:

select new
{
   Duration = (string)f.Element("duration"),
   Cabin = (string)f.Element("class"),
   Price = "$" + (Int32)f.Element("price")
   ImagePath = (string)f.Element("airlineImage")
};

<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false">
   <Columns>
      <asp:ImageField DataImageUrlField="ImagePath" 
                      DataImageUrlFormatString="~/Content/Images/{0}" />
   </Columns>
</asp:GridView>

更新1. 你必须在GridView中替换路径~/Content/Images/{0}到你存储图像的地方。2. 设置autogeneratecolns ="false",这样你的网格将只包含你定义的列。

我认为你最好将路径加载到对象中,然后动态设置图像的src属性:

<img src='<%# Eval("ImageURL") %>' />

相关内容

  • 没有找到相关文章

最新更新