我用Gridview显示数据库中记录,但是有个字段是文件路径,我希望在表格中,文件名直接显示为连接,可以点文件直接下载,应该怎么办?

解决方案 »

  1.   

    直接绑定<asp:BoundField DataField="FILEPATH" HeaderText="文件路径"  DataFormatString="&lt;a href='#'&gt;{0}&lt;/a&gt;">
    或者在后台,组合好下载URL的字符串后,直接赋值给这个单元格也行。
      

  2.   

    直接绑定<asp:BoundField DataField="FILEPATH" HeaderText="文件路径"  DataFormatString="&lt;a href='#'&gt;{0}&lt;/a&gt;">
    或者在后台,组合好下载URL的字符串后,直接赋值给这个单元格也行。
      

  3.   

     <asp:HyperLinkField DataTextField="cTitle" HeaderText="标题" SortExpression="cTitle"
                            DataNavigateUrlFields="userial" DataNavigateUrlFormatString="~/Information/IM_ReceiveInfo.aspx?ID={0}" Target="_self">
                            <ControlStyle Width="300px" />
                        </asp:HyperLinkField>protected void GridViewInformationList_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {//标题
                    string cTitle = ((HyperLink)(e.Row.Cells[1].Controls[0])).Text;                
                    ((HyperLink)(e.Row.Cells[1].Controls[0])).Text = CommonBusinessService.SubStr(cTitle, 100);
                    ((HyperLink)(e.Row.Cells[1].Controls[0])).ToolTip = cTitle;
                    
      

  4.   

    可以这样用,在模板列中下面FileName是文件名,不是路径,根据实际情况填写<asp:GridView ID="GridView1" runat="server">
    <Columns>
    <asp:TemplateField>
       <ItemTemplate>
          <a href='download.ashx?url=<%# Server.UrlEncode(Eval("FileName").ToString()) %>' ><%#Eval("FileName") %> </a>
      </ItemTemplate>
    </asp:TemplateField>
    </Columns>      
    </asp:GridView>
    建立一个download.ashx
    <%@ WebHandler Language="C#" Class="download" %>
    using System;
    using System.Web;
    public class download : IHttpHandler {    public void ProcessRequest (HttpContext context) {
            string url = HttpContext.Current.Server.UrlDecode(context.Request.QueryString["url"]);
            downloadfile(url);
        }    public bool IsReusable {
            get {
                return false;
            }
        }
        public void downloadfile(string s_fileName)
        {
          HttpContext.Current.Response.ContentType = "application/ms-download";
          string s_path = HttpContext.Current.Server.MapPath("~/") + s_fileName; //这里根据实际路径填写
          System.IO.FileInfo file = new System.IO.FileInfo(s_path);
          HttpContext.Current.Response.Clear();
          HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream");
          HttpContext.Current.Response.Charset = "utf-8";
          HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
          HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
          HttpContext.Current.Response.WriteFile(file.FullName);
          HttpContext.Current.Response.Flush();
          HttpContext.Current.Response.Clear();
          HttpContext.Current.Response.End();
        }
      

  5.   


    <asp:GridView ID="GridView1" runat="server" onrowcommand="GridView1_RowCommand"> 
    <Columns> 
    <asp:TemplateField> 
      <ItemTemplate> 
      <asp:LinkButton ID="lnkbtnDown" runat="server" CommandName="DownLoad" 
    Text='<%#Eval("文件名") %>' CommandArgument=<%#Eval("文件路径") %>></asp:LinkButton>
      </ItemTemplate> 
    </asp:TemplateField> 
    </Columns>      
    </asp:GridView> protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                if (e.CommandName == "DownLoad")
                {
                    HttpContext.Current.Response.ContentType = "application/ms-download";
                    string strPath = HttpContext.Current.Server.MapPath("~/") + e.CommandArgument;
                    System.IO.FileInfo file = new System.IO.FileInfo(strPath);
                    HttpContext.Current.Response.Clear();
                    HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream");
                    HttpContext.Current.Response.Charset = "utf-8";
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
                    HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
                    HttpContext.Current.Response.WriteFile(file.FullName);
                    HttpContext.Current.Response.Flush();
                    HttpContext.Current.Response.Clear();
                    HttpContext.Current.Response.End();            }
            }
      

  6.   

    方法都没错,按照楼主说的,要先把文件名提取出来,SubString
      

  7.   

    在gridview中加个超链接列,在databound事件里绑定该字段
      

  8.   

     8               <asp:GridView ID="GridView" runat="server" Width="100%" AutoGenerateColumns="False" AllowPaging="True" OnPageIndexChanging="GridView_PageIndexChanging" PageSize="12" >
     9                 <Columns>
    10                       <asp:BoundField DataField="UserID" HeaderText="UserID" />
    11                       <asp:BoundField DataField="C_Name" HeaderText="中文名字"  />
    12                       <asp:BoundField DataField="E_Name" HeaderText="英文名字"  />   
    13                       <asp:BoundField DataField="salary" HeaderText="薪水" DataFormatString="{0:C}"  HtmlEncode ="False"/>               
    14                       <asp:HyperLinkField DataNavigateUrlFields="C_Name" DataNavigateUrlFormatString="Demo28_Url.aspx?para={0}"  DataTextField="C_Name"
    15                         HeaderText="点击打开新页面" />
    16                       <asp:HyperLinkField DataNavigateUrlFields="UserID" DataNavigateUrlFormatString="Demo28_Url.aspx?para={0}"  DataTextField="C_Name"
    17                         HeaderText="传递不同的参数(UserID)" />
    18                   </Columns>
    19                   <RowStyle HorizontalAlign="Center" />  
    20                   <PagerStyle HorizontalAlign="Right" />
    21               </asp:GridView>
    请参考GridView控件实现HyperLinkField列打开新页面