如何从服务器floder gridview的显示图像? [英] How to display image in gridview from server floder?

查看:88
本文介绍了如何从服务器floder gridview的显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误code

I get error following code

 <asp:TemplateField HeaderText="Image">
<ItemTemplate>
   <asp:Image ID="img" runat="server" ImageUrl="~/Attachment/<%#Eval("Image") %>" />
</ItemTemplate>
</asp:TemplateField>

错误
分析器错误信息:服务器标签不规范

error Parser Error Message: The server tag is not well formed.

推荐答案

第一次尝试这样的:

 <asp:TemplateField HeaderText="Image">
 <ItemTemplate>
    <asp:Image ID="img" runat="server" ImageUrl="<%# Page.ResolveClientUrl(String.Format("~/Attachment/{0}",Eval("Image"))) %>" />
    </ItemTemplate>
 </asp:TemplateField>


有另一种选择做服务器不在客户端。如果您需要设置图片网址运行时它是非常有用的。


There is another option to do that in the server not in the client. its is useful if you need to set the image url in run time.

 <asp:GridView runat="server" ID="gvActivities" AllowSorting="true" AllowPaging="true"
        PageSize="25" AutoGenerateColumns="false" Width="100%" OnSorting="gvActivities_Sorting"
        OnRowDataBound="gvActivities_RowDataBound">
        <Columns>

            <asp:TemplateField HeaderText='Image' HeaderStyle-Width="4%"
                SortExpression="ActivityType">
                <ItemTemplate>
                    <asp:Image ID="ImageType" runat="server" AlternateText='<%# Eval("Type") %>' />
                </ItemTemplate>
            </asp:TemplateField>

有你所看到的,我使用的是OnRowDataBound设置图像的URL。

Has you can see, i am using the OnRowDataBound to set the image url.


  1. 我不是设置在客户端中IMAGEURL。

  2. 我检查如果该行类型数据行。

  3. 然后我创建的图像,并把图像从笑容进去。看到我使用FindControl方法。了将ImageType是在网格中的图像的id

  4. 然后我设置ImageUrl属性

  1. I am not setting the ImageURL in the client.
  2. I am checking if the Row type is a Data Row.
  3. Then i am creating an image and putting the image from the grin into it. see that i am using the FindControl method. the "ImageType" is the id of the image in the grid.
  4. then i am setting the imageURL Property

protected void gvActivities_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
    {
       Image img = (Image)e.Row.Cells[0].FindControl("ImageType");
       img.ImageUrl = Page.ResolveClientUrl("Image URL path);
       img.AlternateText = "Text";
       img.ToolTip = "Tooltip";
    }
}


这篇关于如何从服务器floder gridview的显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆