描述你的问题
我点击删除,但是没有在没有权限的情况下会弹出无法删除的提示,但是数据还是删除了,应该怎么写呢?-
贴上相关代码
<td valign="top" align="center" style="width: 368px; height: 245px;"> <asp:GridView ID="GV" runat="server" AutoGenerateColumns="False" AllowPaging="True" OnPageIndexChanging="GV_PageIndexChanging" OnRowDeleting="GV_RowDeleting" OnRowUpdating="GV_RowUpdating" PageSize="5" OnRowCommand="GV_RowCommand" CellPadding="4" ForeColor="#333333" GridLines="None" AllowSorting="True"> <Columns> <asp:BoundField DataField="TopicId" HeaderText="编号" /> <asp:BoundField DataField="UserLoginName" HeaderText="用户" /> <asp:BoundField DataField="Title" HeaderText="标题" /> <asp:BoundField DataField="CreateTime" HeaderText="发表时间" /> <asp:ButtonField Text="修改" HeaderText="修改" CommandName="Update" /> <asp:HyperLinkField HeaderText="详细信息" DataTextFormatString="详细信息" DataNavigateUrlFormatString="TopicDetail.aspx?topic_id={0}" Text="详细信息" DataNavigateUrlFields="TopicId" /> <asp:ButtonField Text="删除" HeaderText="删除" CommandName="Delete" /> </Columns> <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> <EditRowStyle BackColor="#999999" /> <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> </asp:GridView> <asp:Label ID="LabelPages" runat="server" Width="150px"></asp:Label><asp:HyperLink ID="HyperLinkAddTopic" runat="server" NavigateUrl="TopicAdd.aspx">发表新帖>></asp:HyperLink> </td> </tr> 后台代码 protected void GV_RowCommand(object sender, GridViewCommandEventArgs e) { int index = Convert.ToInt32(e.CommandArgument); //待处理的行下标 int topicId = -1; switch (e.CommandName) { //修改 case "Update": topicId = Convert.ToInt32(GV.Rows[index].Cells[0].Text); Response.Redirect("TopicUpdate.aspx?topic_id=" + topicId); break; //删除 case "Delete": //Response.Write(index); topicId = Convert.ToInt32(GV.Rows[index].Cells[0].Text); Topic topic = new Topic(); topic.LoadData(topicId); topic.Delete(); break; default: break; } } /// <summary> /// 翻页事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GV_PageIndexChanging(object sender, GridViewPageEventArgs e) { GV.PageIndex = e.NewPageIndex; InitData(); } /// <summary> /// 删除前事件,检测用户是否有删除该数据的权限 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GV_RowDeleting(object sender, GridViewDeleteEventArgs e) { string userLoginNameOfTopic = GV.Rows[e.RowIndex].Cells[1].Text.ToString(); //UserLoginName if (userLoginNameOfTopic == "guest" || userLoginNameOfTopic != Session["login_name"].ToString()) { Response.Write("<Script Language=JavaScript>alert('您无权删除!');</Script>"); } Response.Write("<Script Language=JavaScript>document.location.href=document.location.href;</Script>"); }
贴上报错信息
贴上相关截图
已经尝试过哪些方法仍然没解决(附上相关链接)
我在if判断内加入了e.Cancel=true;也没用。Delete还是会执行
试试在
GV_RowDeleting
中,弹出提示的代码附近加一句参考:CancelEventArgs.Cancel 属性