I have a gridview in which there is a column to '
Remove zip file'. The zip file in question is being added by the user to a folder in the application and the name is stored in the database. Each row of the
GridView
has a zip file associated with it. Now, when I click on the '
Remove Zip file' button in
GridView
, I want to delete this zip file. Here is the code I am using.
<asp:GridView ID="gvrecord" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderStyle="None" BorderWidth="1px" CellPadding="4" onrowcancelingedit="gvrecord_RowCancelingEdit"
onrowdatabound="gvrecord_RowDataBound" onrowdeleting="gvrecord_RowDeleting"
onrowediting="gvrecord_RowEditing" onrowupdating="gvrecord_RowUpdating" Height="168px"
Width="850px">
<RowStyle BackColor="White"/>
<Columns>
<asp:TemplateField HeaderText="PROJECT ID" ItemStyle-Font-Size="12px">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#Bind("ProjectId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="JOB TITLE" ItemStyle-Font-Size="12px">
<ItemTemplate>
<asp:Label ID="lbljob" runat="server" Text='<%#Bind("jobtitle") %>'></asp:Label>
</ItemTemplate>
.
.
.
.
<asp:TemplateField ItemStyle-Font-Size="12px">
<ItemTemplate>
<asp:LinkButton ID="lnkremovezip" runat="server" OnClick="lnkremovezip_Click">Remove Zip File</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
and here is the code for the
lnkremovezip_Click()
protected void lnkremovezip_Click(object sender, EventArgs e)
{
LinkButton lnkzipremove = (LinkButton)sender;
GridViewRow row = (GridViewRow)lnkzipremove.NamingContainer;
Label lblzipfile = (Label)row.FindControl("lblzip");
string path = "~/ZipFiles/" + lblzipfile.Text;
FileInfo file = new FileInfo(path);
if (file.Exists)
{
file.Delete();
}
}
I put breakpoints in the code and found that when the execution gets to the 'if' statement, the condition is false and it doesn't execute the
file.Delete()
.The zip file is present in the
/ZipFiles/
folder.
Can somebody please help me figure out where I've gone wrong?