【问题标题】:LINQ to XML and GridView.ImageFieldLINQ to XML 和 GridView.ImageField
【发布时间】:2011-04-22 09:26:29
【问题描述】:

我想知道如何根据 xml 文档中的 imageurl 将图像添加到 gridview。到目前为止我...

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

在拇指字段中显示 url 效果很好,但是我如何将其更改为图像字段而不是显示这个?

【问题讨论】:

    标签: c# linq gridview linq-to-xml imagefield


    【解决方案1】:

    标记:

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

    你有什么问题?

    【讨论】:

    • 是的,我可以做这很好,但只希望 gridview 显示图像而不是它当前执行的文件路径
    • @Kev:图像的路径如何?它应该相对于包含 GridView 的控件。或服务器相关的,例如~/Images/foo.gif
    • gridview 现在有两列。一个是图片的网址,旁边是绑定到它的图片
    • @Kev:图像的路径是相对的、绝对的还是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    • 2013-02-17
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多