求救,很急,CHEKBOX控件选择问题

kacnainai 2012-08-17 08:23:49
前台定义了14个checkbox控件,现在用户要选择6个控件来进行投票(只能选6个,不能多不能少),选上后点击投票按钮,被选中的人数据库票数就加1。

我自己写了段代码,运行后虽然没有报错,可是却得不到我想要的结果。

拜托各位高手,帮忙看下,或者写段代码分享下。再次感谢!

我的代码:
 protected void Button1_Click(object sender, EventArgs e)
{


//投票防作弊
if (Session["UserID"] == null )
{
Response.Write("<script language=javascript>alert('您还未登录');</script>");
Response.Redirect("<script>window.location.href='Default.aspx';</script>");
}
else
{
if (Convert.ToBoolean(Session["Role"]))//已经投过票
{
Response.Write("<script language=javascript>alert('你已经成功投过票,不能重新投票!');</script>");
return;
}
else//还没投过票,执行投票操作,票数加1
{
int num1 = 0;
foreach (Control c in this.Controls)
{
if (c is CheckBox && ((CheckBox)c).Checked)
{
num1++;
}
}

if (num1 > 0)
{
if (num1 < 6)
{
Response.Write(CC.MessageBox(string.Format("共有{0}人被选中,请继续选足6人", num1.ToString())));
}
else if (num1 == 6)
{

int i = 1; string[] strarr = new string[5]; int j = 0;

foreach (Control a in this.Controls)
{
if (a is CheckBox && ((CheckBox)a).Checked&&(i < 15))
{
string P_Str_voteItemID = ((CheckBox)a).ID;
string P_Str_cmdtxt = "update tb_VoteItem set voteTotal=voteTotal+1 where voteItemID=" + P_Str_voteItemID;
strarr[j] = Convert.ToString(i);

CC.ExSql(P_Str_cmdtxt);
i++;
j++;
}

else
{
i = i + 1;
}
}

string ttt = string.Join(",", strarr );


string P_Str_cmdtxt1 = "update tb_User set Role=1 where UserID=" + M_Str_voteID;
string P_Str_cmdtxt2 = "update tb_User set Choose= '" + ttt + "' where UserID=" + M_Str_voteID;
string P_Str_cmdtxt3 = "update tb_User set VoteDate='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' where UserID=" + M_Str_voteID;
CC.ExSql(P_Str_cmdtxt1);
CC.ExSql(P_Str_cmdtxt2);
CC.ExSql(P_Str_cmdtxt3);
Response.Write("<script>alert('投票成功,感谢您的支持!');</script>");



}
else
{
Response.Write(CC.MessageBox("您选择的选手已经超过6名"));
}
}
else
{
Response.Write(CC.MessageBox("请选择参赛选手"));
}
}
}
}


...全文
147 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
kacnainai 2012-08-18
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]
你判断的num1值为0
[/Quote]


num1为0 ,说明我的代码写的有问题,可我写了“num1++”,num1的值没有变吗?
holicc 2012-08-17
  • 打赏
  • 举报
回复
你判断的num1值为0
holicc 2012-08-17
  • 打赏
  • 举报
回复
判断数量,尽量在客户端写,下面是代码:

<script language="javascript" type="text/javascript">
function check()
{
var x=0;
var chkArray = window.document.getElementsByTagName("input");
for(var i = 0; i< chkArray.length; i++)
{
if(chkArray[i].type != "checkbox")
{
continue;
}
if(chkArray[i].checked)
{
x++;
}
}
if(x<6 || x>6)
{
alert('必须是6个!');return false;
}
}
</script>

在button1click中,我改了一下,

protected void Button1_Click(object sender, EventArgs e)
{

string sql = "";
if (CheckBox1.Checked)
{
sql = sql + " update tb_VoteItem set voteTotal=voteTotal+1 where voteItemID=" + CheckBox1.ID;
}
if (CheckBox2.Checked)
{
sql = sql + " update tb_VoteItem set voteTotal=voteTotal+1 where voteItemID=" + CheckBox2.ID;
}
if (CheckBox3.Checked)
{
sql = sql + " update tb_VoteItem set voteTotal=voteTotal+1 where voteItemID=" + CheckBox3.ID;

}
if (CheckBox4.Checked)
{
sql = sql + " update tb_VoteItem set voteTotal=voteTotal+1 where voteItemID=" + CheckBox4.ID;
}
if (CheckBox5.Checked)
{
sql = sql + " update tb_VoteItem set voteTotal=voteTotal+1 where voteItemID=" + CheckBox5.ID;
}
if (CheckBox6.Checked)
{
sql = sql + " update tb_VoteItem set voteTotal=voteTotal+1 where voteItemID=" + CheckBox6.ID;
}
if (CheckBox7.Checked)
{
sql = sql + " update tb_VoteItem set voteTotal=voteTotal+1 where voteItemID=" + CheckBox7.ID;
}
if (CheckBox8.Checked)
{
sql = sql + " update tb_VoteItem set voteTotal=voteTotal+1 where voteItemID=" + CheckBox8.ID;
}
if (CheckBox9.Checked)
{
sql = sql + " update tb_VoteItem set voteTotal=voteTotal+1 where voteItemID=" + CheckBox9.ID;
}
if (CheckBox10.Checked)
{
sql = sql + " update tb_VoteItem set voteTotal=voteTotal+1 where voteItemID=" + CheckBox10.ID;
}
if (CheckBox11.Checked)
{
sql = sql + " update tb_VoteItem set voteTotal=voteTotal+1 where voteItemID=" + CheckBox11.ID;
}
if (CheckBox12.Checked)
{
sql = sql + " update tb_VoteItem set voteTotal=voteTotal+1 where voteItemID=" + CheckBox12.ID;
}
if (CheckBox13.Checked)
{
sql = sql + " update tb_VoteItem set voteTotal=voteTotal+1 where voteItemID=" + CheckBox13.ID;
}
if (CheckBox14.Checked)
{
sql = sql + " update tb_VoteItem set voteTotal=voteTotal+1 where voteItemID=" + CheckBox14.ID;
}
CC.ExSql(sql);

string P_Str_cmdtxt1 = "update tb_User set Role=1 where UserID=" + M_Str_voteID;
string P_Str_cmdtxt2 = "update tb_User set Choose= '" + ttt + "' where UserID=" + M_Str_voteID;
string P_Str_cmdtxt3 = "update tb_User set VoteDate='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' where UserID=" + M_Str_voteID;
CC.ExSql(P_Str_cmdtxt1);
CC.ExSql(P_Str_cmdtxt2);
CC.ExSql(P_Str_cmdtxt3);
Response.Write("<script>alert('投票成功,感谢您的支持!');</script>");

}

时间仓促,不怎么考虑代码精简了,你看看能用不能用。完了
子夜__ 2012-08-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

运行时总是会显示“请选择参赛选手”,不管怎么选都这样
[/Quote]
打个断点 一步步走走看?
kacnainai 2012-08-17
  • 打赏
  • 举报
回复
Session["UserID"] == null 是判断有没有登陆的条件,如果在没有登陆的情况下我按了“投票按钮”就会出现页面“乱码+404+乱码”的情况
kacnainai 2012-08-17
  • 打赏
  • 举报
回复
运行时总是会显示“请选择参赛选手”,不管怎么选都这样
icefisher 2012-08-17
  • 打赏
  • 举报
回复
达不到你要的效果,指的是哪一部分逻辑出错呢?设置断点先确定哪一部分的问题。在我看来,这一句有点问题:
if (a is CheckBox && ((CheckBox)a).Checked&&(i < 15))不成立时,你的代码中i也是增加1的,这样如果页面有其它控件的情况下,是否会造成i>15时,仍有页面checkbox未被遍历到?
还有
string P_Str_cmdtxt1 = "update tb_User set Role=1 where UserID=" + M_Str_voteID;
string P_Str_cmdtxt2 = "update tb_User set Choose= '" + ttt + "' where UserID=" + M_Str_voteID;
string P_Str_cmdtxt3 = "update tb_User set VoteDate='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' where UserID=" + M_Str_voteID;
CC.ExSql(P_Str_cmdtxt1);
CC.ExSql(P_Str_cmdtxt2);
CC.ExSql(P_Str_cmdtxt3);
为什么不写成一条SQL语句执行呢?

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧