2014-09-21 36 views
0

我正在嘗試管理網站中的文件和文件夾。我試圖在列表框中列出文件夾,並在另一個列表框中的當前文件夾中列出文件。在ASP.NET C中使用GridView處理文件時產生索引錯誤#

我決定用datagridview來做。最後我找到了一個僅用於列表的文件和文件夾示例。教程是在這裏:http://www.4guysfromrolla.com/articles/090110-1.aspx

我已經添加編輯和刪除列到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(); 
    } 
+0

RowIndex在失敗行的值是什麼? – Steve 2014-09-21 18:11:23

+0

@Steve我得到所有行的錯誤 – zkanoca 2014-09-21 18:20:38

回答

1

GridView控件數組DataKeys可當你在你的標記設置DataKeyNames

看來你錯過

<asp:GridView ID="gvFiles" runat="server" AutoGenerateColumns="False" 
..... 
DataKeyNames="Name" 
..... 
/> 

我假定列名是你的唯一關鍵的工作。