从Arraylist中删除行

时间:2012-07-11 14:03:04

标签: c# asp.net

有没有人有任何帮助,我使用Arraylist在会话中存储细节。当项目被添加到购物车我希望能够有一些功能,我开始使用删除按钮(最终我也想要一个编辑按钮)所以我尝试了两种方式的删除按钮,既没有为我工作, 第一次尝试:

sc.aspx page

    <asp:TemplateField> 
    <ItemTemplate> 
        <asp:Button ID="btnDelete" runat="server" CommandArgument='<%# ((GridViewRow)Container).RowIndex %>' CommandName="deleterow" Text="Delete" /> 
    </ItemTemplate>
</asp:TemplateField>'

使用:

'onrowcommand="DeleteRowBtn_Click"'

sc.aspx.cs

'protected void DeleteRowBtn_Click(object sender, GridViewCommandEventArgs e)
{
    int rowIndex = Convert.ToInt32(e.CommandArgument); 
}' 

第二次尝试:

sc.aspx

'OnRowDeleting="GridView1_RowDeleting"'

 '<asp:CommandField ShowDeleteButton="True" />'

sc.aspx.cs

'protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    ArrayList remove = (ArrayList)Session["aList"];
    DataRow dr = remove.Rows[e.RowIndex];
    remove.Rows.Remove(dr);
    GridView1.EditIndex = -1;
    FillShopCart();
}'

1 个答案:

答案 0 :(得分:1)

这不起作用,因为ArrayList没有Rows属性,但有indexer个数组:

ArrayList remove = (ArrayList)Session["aList"];
DataRow dr = remove.Rows[e.RowIndex];

所以这可以工作

DataRow dr = (DataRow)remove[e.RowIndex];

Sidenote :如果您至少使用.NET 2.0并使用类似ArrayList的强类型集合,则应避免使用HashTable(或List<T>)或Dictionary<TKey, TValue>代替。