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

发布于 2024-11-02 13:34:56 字数 1030 浏览 1 评论 0原文

我想在航空公司名称下方显示航空公司图像。我已将航空公司图像的路径存储在 XML 文件中。我将如何使用 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();

I want to display an airline image underneath an airline name. I have stored paths in my XML file for the airlineImage. How would I go about displaying the image in my query using LINQ?

Here's my code so far:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

り繁华旳梦境 2024-11-09 13:34:56

试试这个:

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. 设置 AutoGenerateColumns="false",以便您的网格将仅包含您已定义的列。

Try this:

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>

UPDATE
1. You have to replace path ~/Content/Images/{0} in GridView to where you store your images.
2. Set AutoGenerateColumns="false" so your grid will only contains colums that you have defined.

丶视觉 2024-11-09 13:34:56

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

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

I think you would be best served by loading the path into your object then setting the src attribute of the image dynamically:

<img src='<%# Eval("ImageURL") %>' />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文