C# gridview中的超链接列
我有一个文件夹,其中包含不同类型的文件,如doc、xls、ppt等。我的gridview显示文件名、ID、文件名和类型。我想将文件名列设置为超链接。超链接列是否可能同时充当超链接和选定索引?我的意思是,当我点击文件名时,它不应该把我带到另一个页面,而是打开我点击的文件?我在gridview中使用了一个commandfield,文本作为视图,它将该列的所有索引显示为视图。但现在我不想这样。相反,我希望超链接字段充当那个commandfield。可能吗C# gridview中的超链接列,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个文件夹,其中包含不同类型的文件,如doc、xls、ppt等。我的gridview显示文件名、ID、文件名和类型。我想将文件名列设置为超链接。超链接列是否可能同时充当超链接和选定索引?我的意思是,当我点击文件名时,它不应该把我带到另一个页面,而是打开我点击的文件?我在gridview中使用了一个commandfield,文本作为视图,它将该列的所有索引显示为视图。但现在我不想这样。相反,我希望超链接字段充当那个commandfield。可能吗 我想要的是,如果gridview看起来像这样
我想要的是,如果gridview看起来像这样 如果gridview显示为 Id文件名类型
1单元文档 2木xls 3老虎ppt
我想将cell、wood和tiger显示为超链接,它们不应该将我带到另一个页面,而是应该打开文件夹中的文件您可以创建一个自定义处理程序(.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>
显然,您需要调整上述示例以满足您的特定需求。通过上述方法下载文件是一个完美的例子,说明您应该使用通用的HttpHandler。我真正想要的是,如果gridview看起来像这样,如果gridview显示为Id文件名类型--------------------1单元格文档2 wood xls 3 tiger ppt我想显示单元格,wood和tiger是超链接,他们不应该把我带到另一个页面,相反,他们应该从folderYes打开文件,使用我在回答中描述的方法将防止用户重定向到另一个页面。单击链接将触发“另存为”对话框。我将在稍后发布一个更完整的示例bit@ErOxHi-Erox我正在从共享点列表动态生成网格视图。在SPlist中,我有一个附件,我需要在其中显示该附件或给它一个超链接。我有一个保存附件URL的字符串ath(例如http://sk123/Lists/CapabilityTableSales/attachments/1/Dashboard Solutions.pptx)。虽然我使用了hyperlink字段,但它给出了一个类似“列不匹配”的错误,我如何才能做到这一点?我会尽快发布我的问题的URL!!
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; }
}
}
}