请问怎样在Grid中显示连接?

llsus 2009-07-20 11:31:48
我用Gridview显示数据库中记录,但是有个字段是文件路径,我希望在表格中,文件名直接显示为连接,可以点文件直接下载,应该怎么办?
...全文
93 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
fgmis 2009-07-20
  • 打赏
  • 举报
回复
 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列打开新页面
Adechen 2009-07-20
  • 打赏
  • 举报
回复
在gridview中加个超链接列,在databound事件里绑定该字段
IHandler 2009-07-20
  • 打赏
  • 举报
回复
方法都没错,按照楼主说的,要先把文件名提取出来,SubString
shenweiblue 2009-07-20
  • 打赏
  • 举报
回复
ding
  • 打赏
  • 举报
回复

<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();

}
}
银狐被占用 2009-07-20
  • 打赏
  • 举报
回复
来学习的。
teerhu 2009-07-20
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 gdjlc 的回复:]
可以这样用,在模板列中

下面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

C# code<%@ WebHandler Language="C#" Class="download"%>using System;using System.Web;publicclass download : IHttpHandler {publicvoid ProcessRequest (HttpContext context) {string url= HttpContext.Current.Server.UrlDecode(context.Request.QueryString["url"]);
downloadfile(url);
}publicbool IsReusable {get {returnfalse;
}
}publicvoid 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();
}
}

[/Quote]
支持
幫頂
gdjlc 2009-07-20
  • 打赏
  • 举报
回复
可以这样用,在模板列中

下面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();
}
}

jiangzhe556 2009-07-20
  • 打赏
  • 举报
回复
 <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;
homesos 2009-07-20
  • 打赏
  • 举报
回复
直接绑定

<asp:BoundField DataField="FILEPATH" HeaderText="文件路径" DataFormatString="<a href='#'>{0}</a>">


或者在后台,组合好下载URL的字符串后,直接赋值给这个单元格也行。
homesos 2009-07-20
  • 打赏
  • 举报
回复
直接绑定

<asp:BoundField DataField="FILEPATH" HeaderText="文件路径" DataFormatString="<a href='#'>{0}</a>">


或者在后台,组合好下载URL的字符串后,直接赋值给这个单元格也行。
llsus 2009-07-20
  • 打赏
  • 举报
回复
谢谢各位,我总算会了。
一套Developer Express控件包 For Delphi7Developer Express控件简介Express Scheduler Suite可以建立类似于Outlook日历那样的空间,计划任务。ExpressQuantumGrid Suite一个超级牛的Grid控件,不要告诉我不知道什么是Grid,其Filter功能也是非常厉害的。ExpressBars Suite非常厉害非常厉害的工具条菜单控件。ExpressPrinting System 打印控件,可以和ExpressQuantumGrid 完美连接,还没发现有没有报表功能,要不就是没有报表功能,完美的ExpressQuantumGrid控件就可以代替了。ExpressQuantumTreeList Suite 非常牛的树状控件,跟ExpressQuantumGrid 结合起来很厉害的。ExpressNavBar 是用来做类似于XP下的浏览器左边的导航条的,非常地帅。ExpressLayout Control 好像是界面布局的控件。ExpressDBTree Suite非常好用的数据库树形列表控件。ExpressMasterView专门用作总单细单甚至多层的关系的GRID显示,它的效率比ExpressQuantumGrid Suite还要高,不过功能不是很强。ExpressDBTree Suite 结合数据库表进行像WINDOWS浏览器树结构显示的控件。ExpressOrgChart 结合数据库表进行组织结构显示的控件。ExpressVerticalGrid Suite 表结构以垂直方式(卡片样式)的GRID显示,一般来说,GRID适合用来显示,而VerticalGrid适合用来编辑数据。下面的控件还不知道有什么用,请有知道的介绍一下。ExpressFlowChartExpressSpreadSheet (includes Kylix support) 下面我们开始安装1、 首先关闭delphi7,如果您正在运行着它的话。 2、 安装ExpressLayout Control 1.1.11 for d7 ok3、 安装ExpressBars Suite 5.1.3 ok4、 安装ExpressDBTree Suite Version 1.3.1 ok5、 安装ExpressOrgChart 1.3.2 ok6、 安装ExpressFlowChart.v1.3.2 ok7、 安装DevExpress ExpressMasterView 1.2.1 ok8、 安装ExpressSpreadSheet (includes Kylix support) 1.1.12 ok9、 安装ExpressNavBar v1.3.1 ok10、安装ExpressPrinting System 3.1 ok11、安装ExpressQuantumGrid Suite 5.0 ok12、安装Express Scheduler Suite 1.0 13、安装ExpressQuantumTreeList Suite v4.0.2 14、安装ExpressVerticalGrid Suite 3.0.2 注:经过我反复试验发现Express Scheduler Suite 1.0 ExpressQuantumTreeList Suite v4.0.2 ExpressVerticalGrid Suite 3.0.2 与ExpressQuantumGrid Suite 5.0有冲突只要其有任何两种控件安装,就不能装入扩展控件包ExpressExtendedEditors Library 5,Grid会少很多控件。15、运行Delphi_7,出现第一个提示框后, 按否在Delphi 7通过Tools->Environment Options->Library->Library path菜单路径打开Diredtories窗口。 将含有以下路径: ......Developer Express IncExpressDataControllerDelphi 7Lib ......Developer Express IncXP Theme ManagerDelphi 7Lib ......Developer Express IncCX LibraryDelphi 7Lib ......Developer Express IncExpressEditors Library 5Delphi 7Lib ......Developer Express IncExpressPageControl 2Delphi 7Lib ......Developer Express IncExpressQuantumGrid 5Delphi 7Lib 删除,重新添加为,或保留一份修改为: ......Developer Express IncExpressDataControllerDelphi 7Sources ......Developer Express IncXP Theme ManagerDelphi 7Sources ......Developer Express IncCX LibraryDelphi 7Sources ......Developer Express IncExpressEditors Library 5Delphi 7Sources ......Developer Express IncExpressPageControl 2Delphi 7Sources ......Developer Express IncExpressQuantumGrid 5Delphi 7Sources 18、到Grid 5.0目录下的完美补丁目录19、将以下2个文件:cxExtEditorsVCLD7.bpl dclcxExtEditorsVCLD7.bpl 解压到操作系统的System目录(注:W2K/WinXP应为system32目录)覆盖原文件。 20、将压缩包ExpressEditors Library 5目录解压到Program FilesDeveloper Express Inc下覆盖原安装目录; 21、将压缩包ExpressPageControl 2目录解压到Program FilesDeveloper Express Inc下覆盖原安装目录; 22、将压缩包ExpressQuantumGrid 5目录解压到Program FilesDeveloper Express Inc下覆盖原安装目录; 23、将压缩包Bpl目录解压到BorlandDELPHI7Projects下覆盖原目录。 24、到此全部安装完毕,对于ConvertGrid3Demo和ConvertGrid3MasterDetailDemo还必须同时安装“DevExpress ExpressQuantumGrid Suite v3.22 for Delphi7”,才行!

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧