判断.net多个 checkbox 谁被选中
if (this.CheckBox1.Checked=true){
string strInsert;//插入数据表的标准的SQL语句
strInsert="update anli set timu1 = timu1 + 1 where id = 1";
//string strInsert;//插入数据表的标准的SQL语句
//strInsert="update anli set timu1 = timu1 + 1 where id = 1";
//如何插入?
//连接数据库
string strconn = "server=localhost;uid=sa;pwd=sa;database=vote";
SqlConnection cnn = new SqlConnection(strconn);
cnn.Open();
//声明命令对象并且初始化
SqlCommand cmd = new SqlCommand();
cmd.CommandText = strInsert;
cmd.CommandType = CommandType.Text;
cmd.Connection = cnn;
//初始化完成,直接执行命令对象,完成数据的插入工作台
try
{
cmd.ExecuteNonQuery();
//插入数据成功
//Response.Write("<script>alert('恭喜您投票成功!');window.location='Default2.aspx';</script>");
//Response.End();
}
catch (SqlException x)
{
Response.Write("<script>alert('投票失败!请重新选择,多谢配合');history.go(-1);</script>");
Response.End();
}
finally
{
cnn.Close();
}
}
&& if (this.CheckBox2.Checked = true)
{
string strInsert;//插入数据表的标准的SQL语句
strInsert = "update anli set timu2 = timu2 + 1 where id = 1";
//string strInsert;//插入数据表的标准的SQL语句
//strInsert="update anli set timu1 = timu1 + 1 where id = 1";
//如何插入?
//连接数据库
string strconn = "server=localhost;uid=sa;pwd=sa;database=vote";
SqlConnection cnn = new SqlConnection(strconn);
cnn.Open();
--------------------编程问答-------------------- 如何判断哪个checkbox是否被选中,同时选中的数量要求是6个,多选或者不足都不行,同时对选择的投票进行票数的增加,如何写呢,本人菜鸟,刚接触.net --------------------编程问答-------------------- 遍历CheckBox并且记录选中的个数,遍历结束的时候判断是否为6个,还有不明白你写的如何插入是什么意思?怎样执行sql语句还是?如果是,找一个例子好好看一下,还有不必要为每一个选中CheckBox都操作数据库,写一个就可以了。 --------------------编程问答-------------------- 我就是想把选中的结果执行sql语句,好在数据库中更新投票的数量。 --------------------编程问答-------------------- 将checkbox放入容器,foreach(Controls c in 容器)
--------------------编程问答-------------------- 把checkbox遍历一边,然后看下有几个被选中就行了 --------------------编程问答--------------------
bool isValid = false;--------------------编程问答--------------------
isValid = CheckBoxList1.Items.Cast<ListItem>()
.Where(item => (isValid = isValid || (item.Value == "abc" && item.Selected)) || item.Selected)
.Count() == 6 && isValid;
--------------------编程问答-------------------- 实在不行。。你就用if这些判断语录判断好了。。不过比较麻烦而已。。。对于新手还是从基础来吧。。我也是刚学 --------------------编程问答-------------------- if (this.CheckBox1.Checked=true)
bool isValid = false;
int count = 0;
foreach (ListItem item in new CheckBoxList().Items)
{
if (item.Selected)
{
count++;
isValid = isValid || item.Value == "abc";
}
}
isValid = isValid && count == 6;
?
if (this.CheckBox1.Checked==true) --------------------编程问答-------------------- 遍历只能知道个数吧,还是不用遍历好点。 --------------------编程问答-------------------- 遍历得到选中项,设法通过一些属性或者其他获取到目标id,拼接Sql,为选中id的每一项选票+1 --------------------编程问答-------------------- 倾向与用jquery判断并统计数量,后台只处理需要记录的数据提交到数据库
补充:.NET技术 , .NET Framework