| Author: Jaya Kumar 05 Jul 2008 | Member Level: Gold | Rating: Points: 6 |
In Grid View.. U place a Link Button in the Templete Field and the code as below
protected void lnkDownload_Click(object sender, EventArgs e) { try { lblError.Visible = false; LinkButton lnk = (LinkButton)sender; string strFileName =lnk.CommandArgument.ToString(); if (strFileName == "") { ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "clientScript", "alert('File Not Found')", true); } else { //Response.ContentType = "application/octet-stream" Response.Clear(); Response.ClearContent(); Response.ContentType = "application/x-download"; Response.AddHeader("Content-Disposition", "attachment;filename=" + Path.GetFileName(strFileName)); Response.WriteFile(Server.MapPath(strFileName)); Response.End(); } } catch (Exception ex) { lblError.Visible = true; lblError.Text = ex.Message; } }
and in the GridView Row Created event u write this code
if (e.Row.RowType == DataControlRowType.DataRow) { LinkButton lnk = (LinkButton)e.Row.FindControl("lnkDownload"); lnk.CommandArgument = Convert.ToString(GridView1.DataKeys[e.Row.RowIndex].Values["Doc_Path"]); }
|