Client side Code
------------------------------------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvtest" runat="server" AutoGenerateColumns ="false">
<Columns>
<asp:TemplateField HeaderText="S.No.">
<ItemTemplate >
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Link">
<ItemTemplate >
<asp:LinkButton ID="lnk" runat="server" Text="download" OnClick="lnk_Click" ToolTip='<%#Eval("LinkId")%>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
-----Code Behind---------------------------------------------------------------------
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
gvtest.DataSource = bindGrid();
gvtest.DataBind();
}
}
protected void lnk_Click(object sender, EventArgs e)
{
LinkButton lnkbutton = (LinkButton)sender;
string id = lnkbutton.ToolTip.ToString();
String fileName=id.ToString()+".pdf";
FileInfo fl = new FileInfo(Server.MapPath("PDF/" +fileName ));
if (fl.Exists)
{
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.AddHeader("Content-Length", fileName.Length.ToString());
string CId = Request["__EVENTTARGET"];
Response.TransmitFile(fl.FullName);
Response.End();
//string path = fl.ToString();
//ScriptManager.RegisterStartupScript(Page, GetType(), "rs", "window.open('" + fl + "');", true);
}
}
protected DataTable bindGrid()
{
DataTable dt = new DataTable();
dt.Columns.Add("LinkId");
for (int i = 1; i <5; i++)
{
DataRow dr = dt.NewRow();
dr["LinkId"] = i.ToString();
dt.Rows.Add(dr);
dt.AcceptChanges();
}
return dt;
}
}
------------------------------------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvtest" runat="server" AutoGenerateColumns ="false">
<Columns>
<asp:TemplateField HeaderText="S.No.">
<ItemTemplate >
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Link">
<ItemTemplate >
<asp:LinkButton ID="lnk" runat="server" Text="download" OnClick="lnk_Click" ToolTip='<%#Eval("LinkId")%>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
-----Code Behind---------------------------------------------------------------------
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
gvtest.DataSource = bindGrid();
gvtest.DataBind();
}
}
protected void lnk_Click(object sender, EventArgs e)
{
LinkButton lnkbutton = (LinkButton)sender;
string id = lnkbutton.ToolTip.ToString();
String fileName=id.ToString()+".pdf";
FileInfo fl = new FileInfo(Server.MapPath("PDF/" +fileName ));
if (fl.Exists)
{
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.AddHeader("Content-Length", fileName.Length.ToString());
string CId = Request["__EVENTTARGET"];
Response.TransmitFile(fl.FullName);
Response.End();
//string path = fl.ToString();
//ScriptManager.RegisterStartupScript(Page, GetType(), "rs", "window.open('" + fl + "');", true);
}
}
protected DataTable bindGrid()
{
DataTable dt = new DataTable();
dt.Columns.Add("LinkId");
for (int i = 1; i <5; i++)
{
DataRow dr = dt.NewRow();
dr["LinkId"] = i.ToString();
dt.Rows.Add(dr);
dt.AcceptChanges();
}
return dt;
}
}