protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow gi in GridView1.Rows)
{
Control ctrl = gi.Cells[0].FindControl("CheckBox1");
if (ctrl != null)
{
CheckBox cb = ctrl as CheckBox;
if (Culture != null)
{
if (cb.Checked == true)
{
Members mb = new Members();
mb.UserID = Convert.ToInt32(gi.Cells[0].Text); DAL dal = new DAL();
int b = dal.Delete(mb);
string msg = ""; if (b >= 1)
{
msg = "删除成功!!!";
} else
{
msg = "删除失败!!";
} Common.ShowMsg(msg, this);
this.GridView1.DataBind(); }
}
}
}
}写完 运行后对CheckBox 打勾后ctrl的值一直是False 这是为什么额 ,我自己之前做的都可以额,现在做就不行 想了很久 就是改不出来 在线等呀!!
{
foreach (GridViewRow gi in GridView1.Rows)
{
Control ctrl = gi.Cells[0].FindControl("CheckBox1");
if (ctrl != null)
{
CheckBox cb = ctrl as CheckBox;
if (Culture != null)
{
if (cb.Checked == true)
{
Members mb = new Members();
mb.UserID = Convert.ToInt32(gi.Cells[0].Text); DAL dal = new DAL();
int b = dal.Delete(mb);
string msg = ""; if (b >= 1)
{
msg = "删除成功!!!";
} else
{
msg = "删除失败!!";
} Common.ShowMsg(msg, this);
this.GridView1.DataBind(); }
}
}
}
}写完 运行后对CheckBox 打勾后ctrl的值一直是False 这是为什么额 ,我自己之前做的都可以额,现在做就不行 想了很久 就是改不出来 在线等呀!!
你先把复选框默认都设置为True,看看结果如何。
你的checkbox是否用的服务器控件?
if (!IsPostBack)
{
//GridView1绑定..
}
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:BoundField DataField="UserID" HeaderText="ID" />
<asp:BoundField DataField="UserName" HeaderText="用户名" />
<asp:BoundField DataField="UserSex" HeaderText="性别" />
<asp:BoundField DataField="Email" HeaderText="电子邮箱" />
<asp:BoundField DataField="Reloe" HeaderText="角色" />
<asp:BoundField DataField="RegTime" HeaderText="注册时间" />
<asp:TemplateField HeaderText="选项">
<ItemTemplate>
<asp:CheckBox ID="Check" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="删除">
<ItemTemplate>
<asp:Button ID="butDel" runat="server" Text="删除" CommandArgument='<%# Eval("UserID") %> ' CommandName="del" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="删除" />
</div>
</form>
</body>
</html>
if (gi.RowType==DataControlRowType.DataRow)
foreach (GridViewRow item in this.GridView1.Rows)
{
HtmlInputCheckBox hicb = item.Cells[0].FindControl("cbSelect") as HtmlInputCheckBox;
if (hicb.Checked)
{..}
}还有就是把页面上的<asp:CheckBox ID="cbSelect" runat="server" />
改成<input id="cbSelect" value='<%# Eval("id") %>' runat="server" type="checkbox" />
Common.ShowMsg(msg, this);
this.GridView1.DataBind();看看数据库中删除成功没有。