GridView+checkbox的问题

a377493206 a377493206 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
//删除
protected void Button5_Click(object sender, EventArgs e)
{
try
{
int count = this.GridView1.Rows.Count;
for (int i = 0; i < count; i++)
{
CheckBox chk =(CheckBox)this.GridView1.Rows[i].FindControl("Itemchk");

if (chk.Checked)
{
int id = Convert.ToInt32(this.GridView1.Rows[i].Cells[0].Text);
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["meetingConn"].ConnectionString);
conn.Open();
string sqlstr = "delete from meetroom where id='"+id+"'";
SqlCommand cmd = new SqlCommand(sqlstr,conn);
cmd.ExecuteNonQuery();
conn.Close();
Response.Write("<script>alert('删除成功');</script>");
}
this.DataBindMeetroom();
}
}
catch
{ Response.Write("<script>alert('服务器忙,请一会再试');</script>");}

上面是删除按钮的代码。

这是前台的代码:
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="EditRoom.aspx.cs" Inherits="EditRoom" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<table style="width: 491px; height: 87px;">
<tr>
<td align="center" style="width: 563px; color: white; background-color: #0066ff; height: 1px;">
会议室管理--管理会议室</td>
</tr>
<tr>
<td style="width: 563px; height: 98px;" align="center">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCreated="Gv_RowCreated" Width="538px" Height="136px" CellPadding="4">
<Columns>
<asp:BoundField DataField="id" HeaderText="编号" ReadOnly="True"/>
<asp:BoundField DataField="roomname" HeaderText="会议室名称" ReadOnly="True" />
<asp:BoundField DataField="roomaddr" HeaderText="会议室地点" ReadOnly="True" />
<asp:BoundField DataField="roomcount" HeaderText="可容纳人数" ReadOnly="True" />
<asp:TemplateField>
<HeaderTemplate>
编辑
</HeaderTemplate>
<ItemTemplate>
<a href="ChangeRoom.aspx?id=<%#DataBinder.Eval(Container.DataItem,"id") %>">编辑</a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="Itemchk" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="White" />
<PagerStyle BackColor="White" />
<HeaderStyle BackColor="SkyBlue" />
</asp:GridView>
</td>
</tr>
<tr>
<td style="width: 563px">
共有<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>条记录,当前是第<asp:Label
ID="Label2" runat="server" Text="1" Width="15px"></asp:Label>/<asp:Label ID="Label3" runat="server"
Text="Label"></asp:Label>页<asp:Button ID="Button1" runat="server" Text="首页" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="上页" OnClick="Button2_Click" />
<asp:Button ID="Button3" runat="server" Text="下页" OnClick="Button3_Click" />
<asp:Button ID="Button4" runat="server" Text="末页" OnClick="Button4_Click" /></td>
</tr>
<tr>
<td style="width: 563px; height: 20px;">
       <asp:CheckBox
ID="CheckBox1" runat="server" Text="全选" Height="22px" AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged" Width="54px" />
 
<asp:Button ID="Button5" runat="server" Text="删除选定项" OnClick="Button5_Click" /></td>
</tr>
</table>
</asp:Content>

每次一点击删除按钮的时候,CheckBox选定的项自动取消选定的,而且删除时间也没有执行,可是
去掉 if (chk.Checked)这句以后,可以执行删除,但是第一次执行时,删除了所有的id为基数的项,以后在执行的时候则按顺序一条一条的执行。
...全文

相关推荐

评论 9
收藏 已收藏
分享
返回顶部
a377493206
.NET社区 62,271 成员 668,968 内容