I need to download all attachments of a list item from gridview in a visual webpart. I tried hyperlink but unable to get it.

Can anyone tell me how to use anchor tag and use href or any other solution to download attachments from gridview.


You can use this link as an example. His links are for deleting the attachments, but you can change the URL to access the file instead. If you post your source code, I can try and help you.

List Item Attachments in Gridview

  • 1
    Good...have you tried to modify it with what I sent you above?
    – jpollar
    Jan 18 '16 at 15:27
  • Yes but unable to get the URL of Attachments of list item..in gridview Attachments field showing count of attached files..need to give a hyperlink so that attachments of a listitem gets downloaded in zip or any compressed format
    – Satya
    Jan 18 '16 at 18:27
    Markup : 
            <div style="gridstyle">
                <asp:GridView ID="GridView1" runat="server" 
                     BackColor="#DEBA84" BorderColor="#DEBA84"
                     BorderStyle="None" BorderWidth="1px" CellPadding="3" 
                     CellSpacing="2" PageSize="5" Font-Names="Times New 
                     Font-Size="Medium" Width="291px" 
                            <asp:TemplateField HeaderText="S.No">
                                    <%# Container.DataItemIndex+1 %>

             <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True"  
             ForeColor="White" />

              <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
              <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" Font-
              Names="Times New Roman" Font-Size="Medium" />
              <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" 
              ForeColor="White" />
      c# : 

         protected void Load_Items(string filter)
        SPSite site = SPContext.Current.Site;
        SPWeb web = SPContext.Current.Web;
        SPQuery query = new SPQuery();
        if (filter != "")
        query.Query = string.Concat("<Where>", "<Eq>", "<FieldRef 
         Name='Name'/>", "<Value Type='Text'>", filter, "</Value>", " 
         </Eq>", "</Where>");

        query.ViewFields = string.Concat("<FieldRef Name='EntryDate'/>", 
        "<FieldRef Name='ActivityDate'/>", "<FieldRef Name='Location'/>", 
       "<FieldRef Name='ItemSerialNo'/>", "<FieldRef Name='Details'/>", "
        <FieldRef Name='Remarks'/>", "<FieldRef Name='QCOfficer'/>", "
        <FieldRef Name='Attachments'/>");
         query.ViewFieldsOnly = true;

        SPList list1 = web.Lists["Comments"];
        DataTable dt = new DataTable();
        dt = list1.GetItems(query).GetDataTable();

        GridView1.Visible = true;
        GridView1.DataSource = dt;
        GridView1.AutoGenerateColumns = true;


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.