【问题标题】:Hyperlink columns in gridviewgridview 中的超链接列
【发布时间】:2011-11-15 20:59:26
【问题描述】:

我有一个文件夹,其中包含不同类型的文件,如 doc、xls、ppt 等。我的 gridview 显示、ID、文件名和类型。我想将文件名列设为超链接。超链接列是否可以同时作为超链接+选定索引?我的意思是,当我单击文件名时,它不应该将我带到另一个页面,而是打开我单击的文件?我在 gridview 中使用了一个以文本为视图的命令字段,它将该列的所有索引显示为视图。但现在我不想那样。相反,我希望超链接字段充当该命令字段。有可能吗?

我真正想要的是,如果 gridview 看起来像这样 如果gridview显示为

Id 文件名类型


1 个单元格文档

2个木xls

3 老虎ppt

我想将单元格、木头和老虎显示为超链接,它们不应该将我带到另一个页面,而是应该从文件夹中打开文件

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    您可以创建一个自定义处理程序 (.ashx) 文件并相应地设置响应标头信息。这应该注意被重定向到另一个页面。

    1)注册一个通用的HttpHandler来处理下载 (添加>新项目>通用处理程序):

    下载.ashx.cs:

    using System;
    using System.Web;
    
    namespace FileDownloads
    {
        public class Downloads : IHttpHandler
        {
            public void ProcessRequest(HttpContext context)
            {
                var file = context.Request.QueryString["f"];
    
                // Assuming all downloadable files are in a folder called "downloads"
                // located at the root of your website/application...
                var path = context.Server.MapPath(
                    string.Format("~/downloads/{0}", file)
                );
    
                var response = context.Response;
                response.ClearContent();
                response.Clear();
                response.AddHeader("Content-Disposition",
                    string.Format("attachment; filename={0};", file)
                );
                response.WriteFile(path);
                response.Flush();
                response.End();
            }
    
            public bool IsReusable
            {
                get { return false; }
            }
        }
    }
    

    2) 像这样连接您的 GridView:

    defalut.aspx:

    <asp:gridview id="downloadsGridView" runat="server" autogeneratecolumns="false">
        <columns>
            <asp:hyperlinkfield headertext="File Name"
              datatextfield="Name"
              datanavigateurlfields="Name"
              datanavigateurlformatstring="~/Downloads.ashx?f={0}" />
        </columns>
    </asp:gridview>
    

    default.aspx.cs:

    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.IO;
    
    namespace FileDownloads
    {
        public partial class _default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (IsPostBack) return;
    
                var directory = Server.MapPath("~/downloads/");
                var filePaths = Directory.GetFiles(directory);
    
                downloadsGridView.DataSource = filePaths.Select(x => new DLFile
                {
                    Name = x.Split('\\').Last()
                });
                downloadsGridView.DataBind();
            }
    
            public class DLFile
            {
                public string Name { get; set; }
            }
        }
    }
    

    显然,您需要调整上述示例以满足您的特定要求。通过上述方法下载文件是您应该使用通用 HttpHandler 时的一个完美示例。

    【讨论】:

    • 我到底想要的是,如果gridview看起来像这样如果gridview显示为Id Filename Type ---------- ----------- 1 cell doc 2 wood xls 3 tiger ppt 我想将 cell、wood 和 tiger 显示为超链接,它们不应该将我带到另一个页面,而是应该从文件夹
    • 是的,使用我在回答中描述的方法将防止用户被重定向到另一个页面。单击该链接将触发“另存为”对话框。稍后我会发布一个更完整的示例
    • @ErOx 您好 Erox 我正在从共享点列表动态生成网格视图。在 SPlist 中,我有一个附件,我需要在其中显示该附件或提供指向它的超链接。我有一个包含附件 URL 的字符串(例如 http://sk123/Lists/CapabilityTableSales/attachments/1/Dashboard Solutions.pptx)。虽然我使用了超链接字段,但它给出了“列不匹配”之类的错误,我该如何完成呢?我会尽快发布我的问题的网址!!
    猜你喜欢
    • 1970-01-01
    • 2012-05-04
    • 1970-01-01
    • 2011-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    相关资源
    最近更新 更多