C# 用GridView的RowDeletint事件,为什么点击一次删除,会执行2次删除操作,并删除2行记录。
如果删除的是最后一条记录,则会报错,intid=int.Parse(Grid_CodeFile.DataKeys[e.RowIndex].Value.ToString()...
如果删除的是最后一条记录,则会报错,int id = int.Parse(Grid_CodeFile.DataKeys[e.RowIndex].Value.ToString());这里的e.RowIndex会超出索引范围。大侠们,这是为什么么,在线等……
前台:
<asp:GridView ID="Grid_CodeFile" runat="server" BorderWidth="0px"
CellSpacing="1" Width="780px" AutoGenerateColumns="False"
CellPadding="4" CaptionAlign="Left" HorizontalAlign="Left"
DataKeyNames="F_ID" EnableModelValidation="True"
onrowdeleting="Grid_CodeFile_RowDeleting"
onrowediting="Grid_CodeFile_RowEditing">
<EmptyDataTemplate></EmptyDataTemplate><Columns>
<asp:CommandField ButtonType="Image" CancelImageUrl="~/Images/web_icon_001.gif" DeleteImageUrl="~/Images/web_icon_009.gif"
EditImageUrl="~/Images/web_icon_018.gif" ShowDeleteButton="True" ShowEditButton="True" UpdateImageUrl="~/Images/web_icon_025.gif" CancelText="Cancel" DeleteText="Delete" EditText="Edit" UpdateText="Save">
<HeaderStyle BackColor="#006699" Font-Bold="False" HorizontalAlign="Left" Width="40px" /><ItemStyle VerticalAlign="Top" BackColor="#F2F2F2" Width="40px" />
<FooterStyle VerticalAlign="Top" BackColor="#F2F2F2" /> </asp:CommandField>
</Columns></asp:GridView> 展开
前台:
<asp:GridView ID="Grid_CodeFile" runat="server" BorderWidth="0px"
CellSpacing="1" Width="780px" AutoGenerateColumns="False"
CellPadding="4" CaptionAlign="Left" HorizontalAlign="Left"
DataKeyNames="F_ID" EnableModelValidation="True"
onrowdeleting="Grid_CodeFile_RowDeleting"
onrowediting="Grid_CodeFile_RowEditing">
<EmptyDataTemplate></EmptyDataTemplate><Columns>
<asp:CommandField ButtonType="Image" CancelImageUrl="~/Images/web_icon_001.gif" DeleteImageUrl="~/Images/web_icon_009.gif"
EditImageUrl="~/Images/web_icon_018.gif" ShowDeleteButton="True" ShowEditButton="True" UpdateImageUrl="~/Images/web_icon_025.gif" CancelText="Cancel" DeleteText="Delete" EditText="Edit" UpdateText="Save">
<HeaderStyle BackColor="#006699" Font-Bold="False" HorizontalAlign="Left" Width="40px" /><ItemStyle VerticalAlign="Top" BackColor="#F2F2F2" Width="40px" />
<FooterStyle VerticalAlign="Top" BackColor="#F2F2F2" /> </asp:CommandField>
</Columns></asp:GridView> 展开
4个回答
展开全部
打个断点跟一下试试,要不代码贴出来
gridview:关键---DataKeyNames="Id"
<asp:GridView ID="gdv_Announce" runat="server" AutoGenerateColumns="False" CellPadding="4"
GridLines="None" PageSize="5" Style="font-size: 12px" Width="100%" OnRowDataBound="gdv_Announce_RowDataBound"
OnRowDeleting="gdv_Announce_RowDeleting" DataKeyNames="Id" ForeColor="Black"
AllowPaging="True">
模版删除按钮:关键---CommandName="delete"
<asp:TemplateField HeaderText="删除">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</EditItemTemplate>
<HeaderStyle BackColor="#99CCFF" ForeColor="Black" Wrap="False" />
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="delete">删除</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
cs:
//gdv_Announce行中的删除
protected void gdv_Announce_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string keyword = gdv_Announce.DataKeys[e.RowIndex].Value.ToString();
string sql_del = "delete from tsmannouncementmanage where id=" + keyword;
dbCon.deleteData(sql_del);
DataBindTogdv_Announce("order by id desc");
}
gridview:关键---DataKeyNames="Id"
<asp:GridView ID="gdv_Announce" runat="server" AutoGenerateColumns="False" CellPadding="4"
GridLines="None" PageSize="5" Style="font-size: 12px" Width="100%" OnRowDataBound="gdv_Announce_RowDataBound"
OnRowDeleting="gdv_Announce_RowDeleting" DataKeyNames="Id" ForeColor="Black"
AllowPaging="True">
模版删除按钮:关键---CommandName="delete"
<asp:TemplateField HeaderText="删除">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</EditItemTemplate>
<HeaderStyle BackColor="#99CCFF" ForeColor="Black" Wrap="False" />
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="delete">删除</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
cs:
//gdv_Announce行中的删除
protected void gdv_Announce_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string keyword = gdv_Announce.DataKeys[e.RowIndex].Value.ToString();
string sql_del = "delete from tsmannouncementmanage where id=" + keyword;
dbCon.deleteData(sql_del);
DataBindTogdv_Announce("order by id desc");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你的代码一定有逻辑问题,还是贴下后台代码吧
更多追问追答
追问
protected void Grid_CodeFile_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = int.Parse(Grid_CodeFile.DataKeys[e.RowIndex].Value.ToString());
bool ifTrue = CodeFileBLL.Instance.DelCodeFile(id);
if (ifTrue)
{
TempDataTabel.Clear();
Grid_CodeFileBind(TempDataTabel);
}
}
}
追答
这段代码应该没问题,PageLoad里面绑定数据是否加了 if(!IsPostBack)的判断,也贴下吧
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
前台要设置DataKeyNames="Id" 设置主键Grid_CodeFile.DataKeys[e.RowIndex].Value 才能使用
更多追问追答
追问
设置了的啊
前台:
DataKeyNames="F_ID"
后台:
Grid_CodeFile.DataSource = dt;
Grid_CodeFile.DataKeyNames = new string[] { "F_ID" };
Grid_CodeFile.DataBind();
追答
是不是你那里调用了两次删除事件 看下你的RowCommod事件是否也调用删除 不行的话F11单步调试看看
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在前面源码删除里面加个属性DataKeys=true
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询