seckel
asked on
How do I delete file listed in a gridview?
I have a gridview that is loaded with file names populated by Microsoft Indexing Service. The files are located in a directory accessible at "~/PDF files/". I want to add the ability to deleted a file from the directory. The markup and the code behind for the gridview is provided in the code snippet section.
I am unable to get the correct value for "fileName"; it should be the file name shown on the row of the gridview. Any suggestion on how to fix my code?
I am unable to get the correct value for "fileName"; it should be the file name shown on the row of the gridview. Any suggestion on how to fix my code?
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="false">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<table width="100%" >
<tr>
<td style="width:50px;">
<asp:ImageButton ID="DeleteImageButton" runat="server" ImageUrl="~/Images/delete.png" CommandName="Delete" />
</td>
<td>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("FilePath") %>' Text='<%# Eval("Name") %>' />
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Private Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
Dim fileName As String = e.Values.Item(1).ToString()
If File.Exists("~/PDF Files/" + fileName) Then
File.Delete("~/PDF Files/" + fileName)
End If
End Sub
ASKER
The problem is not with using the File.Delete method, but with getting the correct fileName from the gridview...
Dim fileName As String = e.Values.Item(1).ToString( )
This code is not getting the correct fileName from the gridview...
Dim fileName As String = e.Values.Item(1).ToString(
This code is not getting the correct fileName from the gridview...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
What woud the code behind look like?
When I try to add an arguement to the GridView1_RowDeleting method, the event handle becomes invalid.
When I try to add an arguement to the GridView1_RowDeleting method, the event handle becomes invalid.
No, in your GridViewDeleteEventArgs you must have a property like "Argument"... in that property you will have your fileName
ASKER
The changes in the code snippet section works.
Markup:
CommandArgument='<%# Eval("Name") %>' OnCommand="DeleteLink"
Code Behind:
Protected Sub DeleteLink(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
Dim fileName As String = e.CommandArgument.ToString
If File.Exists(Server.MapPath("~/PDF Files/" + fileName)) Then
File.Delete(Server.MapPath("~/PDF Files/" + fileName))
End If
End Sub
add
DataKeyNames=Name, OnRowDeleting="GridView_Ro wDeleting" , OnRowCommand="GridView_Row Command"
in the gridview definition section
in the code behind do the following
protected void GridView_RowDeleting (object sender, GridViewDeleteEventArgs e)
{
}
protected void GridView_RowCommand (object sender, GridViewCommandEventArgs e)
{
string fileName = gridView1.DataKeys[Convert .ToInt32 (e.CommandArgument)].Value .ToString ();
// move your delete logic here from your RowDeleting method and then rebind the grid
}
DataKeyNames=Name, OnRowDeleting="GridView_Ro
in the gridview definition section
in the code behind do the following
protected void GridView_RowDeleting (object sender, GridViewDeleteEventArgs e)
{
}
protected void GridView_RowCommand (object sender, GridViewCommandEventArgs e)
{
string fileName = gridView1.DataKeys[Convert
// move your delete logic here from your RowDeleting method and then rebind the grid
}
File.Delete(Server.MapPath