Reputation: 9928
I am trying to manage files and folders in a web site. I have tried to list folders in a listbox and files in current folder in another listbox.
I have decided to do it with datagridview. Finally I have found a sample for files and folders for listing only. Tutorial is here: https://web.archive.org/web/20210513005012/http://www.4guysfromrolla.com/articles/090110-1.aspx
I have added edit and delete columns to gridview.
filemanager.aspx
<asp:GridView ID="gvFiles" runat="server" AutoGenerateColumns="False"
OnPageIndexChanging="gvFiles_PageIndexChanging"
OnSelectedIndexChanged="renFile" OnRowCommand="gvFiles_RowCommand"
OnRowDataBound="gvFiles_RowDataBound" OnRowDeleting="delFile">
<AlternatingRowStyle />
<Columns>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lbFolderItem" CommandName="OpenFolder" CommandArgument='<%# Eval("Name") %>'></asp:LinkButton>
<asp:Literal runat="server" ID="ltlFileItem"></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="FileSystemType" HeaderText="Type" SortExpression="FileSystemType" />
<asp:BoundField DataField="LastWriteTime" HeaderText="Last Modified" SortExpression="LastWriteTime" />
<asp:TemplateField HeaderText="Size" SortExpression="Size" ItemStyle-HorizontalAlign="Right">
<ItemTemplate>
<%# DisplaySize((long?) Eval("Size")) %>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right"></ItemStyle>
</asp:TemplateField>
<asp:CommandField HeaderText="Rename" SelectText="Rename"
ShowSelectButton="True" ButtonType="Link"/>
<asp:CommandField HeaderText="Delete" DeleteText="Delete"
ShowDeleteButton="True" ButtonType="Link" />
</Columns>
</asp:GridView>
filemanager.aspx.cs
public void delFile(object sender, GridViewDeleteEventArgs e)
{
//I have got error:
//System.ArgumentOutOfRangeException:
//Index was out of range.
//Must be non-negative and less than the size of the collection.
string filename = gvFiles.DataKeys[e.RowIndex].Values[0].ToString(); //on this line
string path = Server.MapPath(ConfigurationManager.AppSettings["path"] + "/" + filename);
//actually do not delete. Add a suffix in order not to list it to the user
File.Move(filename, filename + "_del");
PopulateGrid();
}
public void renFile(object sender, GridViewSelectEventArgs e)
{
//I have got error:
//System.ArgumentOutOfRangeException:
//Index was out of range.
//Must be non-negative and less than the size of the collection.
string filename = gvFiles.DataKeys[e.NewSelectedIndex].Values[0].ToString(); //on this line
string path = Server.MapPath(ConfigurationManager.AppSettings["path"] + "/" + filename);
//just for experiment
File.Move(filename, filename + "_ren");
PopulateGrid();
}
Upvotes: 0
Views: 132
Reputation: 216313
The GridView DataKeys array is available when you set the DataKeyNames in your markup.
It seems that you miss
<asp:GridView ID="gvFiles" runat="server" AutoGenerateColumns="False"
.....
DataKeyNames="Name"
.....
/>
I am supposing that the column name is your unique key to work with.
Upvotes: 1