如何使用 LINQ 在查询中显示图像?
我想在航空公司名称下方显示航空公司图像。我已将航空公司图像的路径存储在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
更新
1. 您必须将 GridView 中的路径
~/Content/Images/{0}
替换为存储图像的位置。2. 设置 AutoGenerateColumns="false",以便您的网格将仅包含您已定义的列。
Try this:
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.
我认为最好的方法是将路径加载到对象中,然后动态设置图像的 src 属性:
I think you would be best served by loading the path into your object then setting the src attribute of the image dynamically: