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.
Markup :
<div style="gridstyle">
<asp:GridView ID="GridView1" runat="server"
BackColor="#DEBA84" BorderColor="#DEBA84"
BorderStyle="None" BorderWidth="1px" CellPadding="3"
EnableModelValidation="True"
CellSpacing="2" PageSize="5" Font-Names="Times New
Roman"
Font-Size="Medium" Width="291px"
Onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="S.No">
<ItemTemplate>
<%# Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<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" />
</asp:GridView>
</div>
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;
GridView1.DataBind();
}