<asp:TemplateField HeaderText="Attachment" ItemStyle-VerticalAlign="Middle" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:LinkButton runat="server" ID="likattachment" CommandName="download" CommandArgument='<%#Eval("attachment") %>'> <img src="../icon/paper-clip.png" style="border:0px;" id="image1" /> </asp:LinkButton> </ItemTemplate> <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle> </asp:TemplateField
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { try { LinkButton lik = (LinkButton)e.CommandSource; if (lik.CommandName == "download") { string filename = "" + e.CommandArgument; if (!string.IsNullOrEmpty(filename)) { Response.Clear(); Response.AddHeader("Content-Disposition", "attachment; filename=" + filename); // Response.AddHeader("Content-Length", file.Length.ToString()); Response.ContentType = "plain/text"; Response.TransmitFile(Server.MapPath("~/upload/" + filename)); } else { Response.Write("<script>alert('No Attachment to download');</script>"); } } } catch(Exception ex) { } }
OnRowDataBound="GridView1_RowDataBound"
GridView
aspx
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { LinkButton lnkAttachment = (LinkButton)e.Row.FindControl("likattachment"); if (!string.IsNullOrEmpty(lnkAttachment.CommandArgument.ToString()) { // Attachment exists, do something. } else { // No Attachments, show icon } }
ImageUrl='<%# Eval("attachment")!=null ? "~/Images/Attachment.gif" : "~/Images/NoAttachment.gif"%>'
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)