0

我有一个网格视图,其中显示文件名、内容类型……上传的 Efile。如何在gridview中的文件名旁边显示上传文件的图标。

<asp:GridView ID="GridViewEfile" runat="server" AutoGenerateColumns="False" CellPadding="4"
                    ForeColor="Black"  
DataKeyNames="FileID" GridLines="None" ShowHeader="False" >
             <AlternatingRowStyle BackColor="Yellow" />
                    <Columns>
                     <asp:TemplateField>
                  <ItemTemplate>

<asp:LinkButton ID="LinkButton1" runat="server" OnCommand="LinkButton1_Command" CommandName="Download" CommandArgument='<%#Eval("FileID")%>'><%#Eval("FileName")%></asp:LinkButton>

                       </ItemTemplate>
              </asp:TemplateField>



                        <asp:BoundField DataField="ContentType" />



                     </Columns>
                     <EditRowStyle BackColor="#7C6F57" />
         <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="Red" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#E3EAEB" />
        <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
                    </asp:GridView>

gridview 的数据源代码如下:

var queryefile = from efile in _DataContext.tblFiles
                     where ((efile.DocId == _DocId) && (efile.TransId == _TransmittalId))
                     select efile;
    GridViewEfile.DataSource = queryefile;

    GridViewEfile.DataBind();
4

1 回答 1

1

您可以参考下面的代码来实现这一点

。CS

using System.Drawing;
using System.IO;

 public string GetIconFromFile()
{
   Icon ic = Icon.ExtractAssociatedIcon(Server.MapPath (".")+"/Files/Test.txt");
   string imagePath=Server.MapPath(".") + "/Images/Test.ico";
   if (ic != null)
   {
      using (FileStream stream = new FileStream(imagePath, FileMode.OpenOrCreate))
       {
           ic.Save(stream);
       }
   }
    return imagePath ;

}

protected void GridViewEfile_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
        img = (System.Web.UI.WebControls.Image)e.Row.FindControl("Image1");
        img.ImageUrl = GetIconFromFile();
    }
}

.aspx

<Columns>
   <asp:TemplateField>
     <ItemTemplate>
        <asp:Image ID="Image1" runat="server" />
          <asp:LinkButton ID="LinkButton1" runat="server" OnCommand="LinkButton1_Command" CommandName="Download" CommandArgument='<%#Eval("FileID")%>'><%#Eval("FileName")%></asp:LinkButton>
      </ItemTemplate>
    </asp:TemplateField>
  <asp:BoundField DataField="ContentType" />
</Columns>
于 2012-11-17T13:18:14.593 回答