单击gridview中的linkbutton时无法从文件夹中删除zip文件 [英] Cannot remove zip file from folder on click of linkbutton in gridview

查看:77
本文介绍了单击gridview中的linkbutton时无法从文件夹中删除zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridview,其中有一列删除zip文件。有问题的zip文件由用户添加到应用程序中的文件夹,名称存储在数据库中。 GridView 的每一行都有一个与之关联的zip文件。现在,当我点击 GridView 中的'删除Zip文件'按钮时,我想删除此zip文件。这是我正在使用的代码。



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>





。< br $> b $ b。




.
.
.
.

<asp:TemplateField ItemStyle-Font-Size="12px">
                   <ItemTemplate>
                       <asp:LinkButton ID="lnkremovezip" runat="server" OnClick="lnkremovezip_Click">Remove Zip File</asp:LinkButton>
                   </ItemTemplate>
                   </asp:TemplateField>



这里是 lnkremovezip_Click的代码( )


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();
   }
}





我在代码中放置了断点,发现当执行到达'if '语句,条件为false,它不执行 file.Delete()。该zip文件存在于 / ZipFiles / 文件夹。



有人可以帮我弄清楚哪里出错了?



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?

推荐答案

试试这个你缺少Server.MapPath



try this you are missing Server.MapPath

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(Server.MapPath(path));
if (file.Exists)
{
file.Delete();
}
}


嘿伙计们,

我想出了问题所在。只需要将 Server.MapPath 添加到路径中。



Hey Guys,
I figured out the problem. Just needed to add Server.MapPath to the path.

FileInfo file=new FileInfo(Server.MapPath(path));


这篇关于单击gridview中的linkbutton时无法从文件夹中删除zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆