Download file using dynamically created Link Button in asp table control in asp.net

Posted by Sriramramesh1610-23884 under ASP.NET on 8/11/2017 | Points: 10 | Views : 331 | Status : [Member] | Replies : 0
Hi All,

I am trying to generate table with 2 columns, one label and one linkbutton. Data getting populated from database. my goal when click on link button respective file download/view. Data populating normally but link button is not working. when click on link button file path is showing null. Pleas suggest here is the sample code,

string strPath = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{ base.InitializePage();
GenerateTab(); }
protected void GenerateTab ()
{ string datetime, Type, lname, lblname, strPath;
DateTime dt;
//Stored Procedure executes and returns result to resultset qr

//Table
Table t = new Table();
for (int x = 0; x < qr.Rows.Count; x++){
Hashtable row = (Hashtable)qr.Rows[x];
dt = Convert.ToDateTime(row["DateCreated"].ToString());
strPath = row["FileName"].ToString();
TableRow tr = new TableRow();
for (int i = 0; i < 2; i++)
{ if (i == 0)
{
TableCell tc0 = new TableCell();
tc0.ID = "tc0" + i.ToString();
lblname = "lbl0" + x.ToString();
Label lbl0 = new Label();
lbl0.ID = lblname;
lbl0.Text = dt.ToString();
tc0.Controls.Add(lbl0);
tr.Cells.Add(tc0);
}
if (i == 1)
{
TableCell tc1 = new TableCell();
tc1.ID = "tc1" + i.ToString();
LinkButton lbtn = new LinkButton();
lname = "lbtn" + x.ToString();
lbtn.ID = lname;
lbtn.Text = row["Subject"].ToString(); lbtn.Click += new EventHandler(lbtn_Click);
tc1.Controls.Add(lbtn);
tr.Cells.Add(tc1);
}
}
t.Controls.Add(tr);
this.form1.Controls.Add(t);
}
catch (Exception ex)
{}
}
protected void lbtn_Click(Object sender, EventArgs e)
{
Response.AppendHeader("content-disposition", "attachment; filename=" + strPath);
Response.ContentType = "application/octet-stream";
Response.WriteFile(strPath);
Response.End();
}



Thanks,




Responses

(No response found.)

Login to post response