我目前正在开发留言系统,我需要管理员删除特定的日志。问题是我试图删除它时出现错误。c#Datalist删除特定行。
的错误是:Index was outside the range. It may not be negative and must be less than the crowd size. Parameter name: index
我的代码:asp.net
<asp:DataList ID="guestBookDataList" runat="server" ondeletecommand="guestBookDataList_DeleteCommand">
<ItemTemplate>
<div id="guest-books">
<span class="guestBook-Id"> # <%#DataBinder.Eval(Container.DataItem, "id") %></span><span class="guestBook-Name">Name: <%#DataBinder.Eval(Container.DataItem, "Name") %> </span>
<span class="guestBook-Date"> Date: <%#DataBinder.Eval(Container.DataItem, "Date") %> </span> <br />
<span class="guestBook-Text"> <%#DataBinder.Eval(Container.DataItem, "text") %> </span>
<asp:LinkButton ID="LinkButtonDelete" CssClass="gastbok-remove" runat="server" CommandName="Delete">Delete<%#DataBinder.Eval(Container.DataItem, "id") %></asp:LinkButton>
</div>
</ItemTemplate>
</asp:DataList>
我的LinkButton是在数据列表的每个项目的删除按钮。
,这就是给我的错误我的C#代码:
protected void guestBookDataList_DeleteCommand(
object source, DataListCommandEventArgs e)
{
int id = Convert.ToInt32(guestBookDataList.DataKeys[e.Item.ItemIndex]);
db.deleteGeustBookLog(id); //calls the sql command
bindList();
}
Basiclly我只是需要从DATABSE retrive的guestbooklogs ID,所以我可以将其删除。
一直在尝试很多不同的方法,并使用谷歌搜索这个,但我不能得到它的工作。所以现在我在问你。谢谢你的时间。
AHH太感谢你了。你救了我的一天:)。它工作完美 – dumbel 2012-02-14 23:33:33
你能接受答案吗? – 2012-02-15 17:45:05