<asp:TemplateColumn HeaderText="查看">
<ItemTemplate>
<a href='<%#String.Format("download.aspx?filename={0}", DataBinder.Eval(Container.DataItem, "filename2"), DataBinder.Eval(Container.DataItem, "filename"))%>' target=_blank>查看</a>
</ItemTemplate>
</asp:TemplateColumn>
--------------------------------------------
download.aspx
<%@ Page Inherits="www.MyCodeBehind" Src="download.cs" %>
<html>
<head>
<title>文件</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="Style.css" type="text/css">
<body leftmargin=0 topmargin=0 marginwidth=0 marginheight=0>
<form runat="server">
</form>
</body>
</html>
-------------------------------------------
download.cs
using System;
using System.Data;
using System.Data.OleDb;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
namespace www
{
public class MyCodeBehind : Page
{
public DataGrid MyList;
public DropDownList DropDownList1;
public DropDownList DropDownList2;
public TextBox TextBox1;
public LinkButton btnFirst;
public LinkButton btnPrev;
public LinkButton btnNext;
public LinkButton btnLast;
public Label lblCurrentPage;
public Label lblPageCount;
public Label lblRecordCount;
public TextBox txtIndex;
public Label label1;
public int PageCount,RecordCount;
public DataRow dr;
private void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
System.IO.FileInfo file = new System.IO.FileInfo("D:/webpro/prod/zsfile/");
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
string filename = Request.QueryString["filename"];
string filename2 = Request.QueryString["filename"];
string nowDSN=ConfigurationSettings.AppSettings["scrbb"];
SqlConnection MyConnection=new SqlConnection(nowDSN);
SqlDataAdapter myCommand=new SqlDataAdapter("SELECT filename FROM scpc WHERE filename2='" + filename2 +"'", MyConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "scpc");
//if(ds.Tables["uploadfile"].Rows.Count > 0)
//{
dr = ds.Tables["scpc"].Rows[0];
filename = ds.Tables["scpc"].Rows[0]["filename"].ToString();
//}
//string filename = Server.MapPath("../uploadfile/" + s);
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(filename));
Response.WriteFile(file+filename);
Response.End();
DataBind();
}
}
}
}
-----------------------------------------
方式二:
<asp:TemplateColumn HeaderText="按右鍵下載" SortExpression="filename">
<ItemTemplate>
<a href='<%#String.Format("../uploadfile/{0}", DataBinder.Eval(Container.DataItem, "filename"))%>' target=_blank title="下載"><%#String.Format("{0}", DataBinder.Eval(Container.DataItem, "filename"))%></a>
</ItemTemplate>
</asp:TemplateColumn>