选中该复选框是否被选中或不 - ASP.NET [英] Check whether the checkbox is checked or not - ASP.NET
问题描述
我有以下的code:
(some.aspx.cs)
(some.aspx.cs)
if(Page.IsPostBack)
{
bool apple2 = false;
bool pizza2 = false;
bool orange2 = false;
if (apple.Checked)
apple2 = true;
if (pizza.Checked)
pizza2 = true;
if (orange.Checked)
orange2 = true;
}
(some.aspx)
(some.aspx)
<tr>
<td>Food:</td>
<td>Apple <input type="checkbox" name="food" id="apple" value="apple" runat="server" />Pizza <input type="checkbox" name="food" id="pizza" value="pizza" runat="server" />Orange <input type="checkbox" name="food" id="orange" value="orange" runat="server" /></td>
</tr>
现在,我送布尔变量的SQL数据库。现在的问题是只能用选中框。我的意思是,当你检查一些复选框是将其作为真正的(这是正确的),但是当我取消他们来说,这仍然是相同的(真)。
Now, i send the Boolean variables to SQL database. The problem is only with unchecked boxes. I mean, when you check some checkboxes it sends it as true (and that's right) but when i uncheck them it remains the same (true).
增加:
为什么太少?
这里有一个查询......这里没有什么特别
Addition: Why too little? here's a query...nothing special here
string q = string.Format(@"UPDATE tblUsers SET ......., apple='{8}', orange='{9}' WHERE id='{10}'", ...., apple2, orange2, id);
lib.sql_query(q); // using my sql library...
数据类型是位....我用绳子也试过......但没有成功。
the datatype is bit....i tried also with string...but no success
P.S。 - 我也试图与的Request.Form [苹果],并取消选中工作......但遗憾的是检查没有......当我选中该复选框它将引发我一个错误:
P.S. - i also tried with Request.Form["apple"], and the uncheck worked...but unfortunately the check didn't...when i check the checkbox it throws me an error:
Conversion failed when converting the varchar value 'on' to data type bit.
有人?
推荐答案
所以组合很长的时间和其他的东西,它的工作......没有任何的JavaScript和隐藏字段后...
那是的.cs code
So after a long time of combinations and other stuff it worked...Without any javascripts and hidden fields... that's the .cs code
bool apple2 = (Request.Form["apple"] == "on") ? true : false;
bool orange2 = (Request.Form["orange"] == "on") ? true : false;
bool pizza2 = (Request.Form["pizza"] == "on") ? true : false;
这篇关于选中该复选框是否被选中或不 - ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!