Click here to Skip to main content
15,922,419 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have use following code:

XML
<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


and on rowcommand event I have use following code:
C#
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)
        {
        }
    }



Now I want to display default image icon for those records which has no image in attachment.. Also records with attachment display in list but with different icon..
I think it will done by RowDataBound event of gridview I tried but my problem is not solved
Plz suggest me what should do..
Thanks in advance..
Posted

Try the below code...
  1. Add OnRowDataBound="GridView1_RowDataBound" to GridView Markup in aspx Page.
  2. Define the Event in Code Behind page.
    C#
    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
        }
    }
 
Share this answer
 
Other option can be using Ternary Operator, Something like

ImageUrl='<%# Eval("attachment")!=null ? "~/Images/Attachment.gif" : "~/Images/NoAttachment.gif"%>'
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900