Check whether the checkbox is checked or not - ASP.NET
I have the following code:
(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)
<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>
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. - 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.
Somebody?
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;
Unchecked checkboxes are not submitted when a form is posted. You will have to write a workaround.
One method would be to have a hidden field that is populated via the javascript of the checkbox.
Firstly I would tidy your code up at the beginning the if statements are unnecessary:
if (Page.IsPostback)
{
bool appleSelected = apple.Checked;
bool pizzaSelected = pizza.Checked;
bool orangeSelected = orange.Checked;
}
Have you tried using the CheckBox class instead of input?
<asp:CheckBox id="apple" value="apple" runat="server" Checked="True|False" />
<asp:CheckBox id="pizza" value="pizza" runat="server" Checked="True|False" />
<asp:CheckBox id="orange" value="orange" runat="server" Checked="True|False" />
It should work since the datatype is bit...at least when you pass the bool to a stored procedure.
Since you have the SQL update statement in your code, try converting the bool to a 0 or 1.
Int16 iApple = (apple2) ? 1 : 0;
Int16 iOrange = (orange2) ? 1 : 0;
string query = string.Format(@"UPDATE tblUsers SET ......., apple='{8}', orange='{9}' WHERE id='{10}'", ...., iApple, iOrange, id);
lib.sql_query(q);
using System.Data.SqlClient;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=RND3 " + "\\" + " SQLEXPRESS;Initial Catalog=SSSolutionFiles;Integrated Security=True");
public void displaygrid()
{
SqlDataAdapter da = new SqlDataAdapter("select * from userfile", con);
DataSet ds = new DataSet();
da.Fill(ds, "p");
GridView1.DataSource = ds.Tables["p"];
GridView1.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
//Label1.Text = txtUserName.Text + "<br>" + txtPassword.Text + "<br>" + txtConfirmPassword.Text + "<br>" + txtConfirmationNumber.Text;
displaygrid();
if (!IsPostBack)
BindDropDownListData();
}
public void BindDropDownListData()
{
//SqlConnection con = new SqlConnection("Data Source=RND3 " + "\\" + " SQLEXPRESS;Initial Catalog=SSSolutionFiles;Integrated Security=True");
//SqlConnection mySqlConnection = new SqlConnection();
{
try
{
con.Open();
SqlCommand mySqlCommand = new SqlCommand("Select username from userfile ", con);
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(mySqlCommand);
DataSet myDataSet = new DataSet();
mySqlDataAdapter.Fill(myDataSet);
//DropDownList1.DataSource = myDataSet;
//DropDownList1.DataTextField = "username";
//DropDownList1.DataValueField = "username";
//DropDownList1.DataBind();
CheckBoxList1.DataSource = myDataSet;
CheckBoxList1.DataTextField = "username";
CheckBoxList1.DataValueField = "username";
CheckBoxList1.DataBind();
}
catch (Exception ex)
{
Label1.Text = ex.Message;
}
finally
{
con.Close();
}
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("insert into userfile values('" + txtUserName.Text + "','" + txtPassword.Text + "','" + txtConfirmPassword.Text + "','" + txtConfirmationNumber.Text + "')", con);
con.Open();
cmd.ExecuteScalar();
displaygrid();
BindDropDownListData();
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("update userfile set confirmnumber='" + txtConfirmationNumber.Text + "', password='" + txtPassword.Text + "',confirmpassword='" + txtConfirmPassword.Text + "' where username='" + txtUserName.Text + "' ", con);
con.Open();
cmd.ExecuteScalar();
displaygrid();
}
protected void btnDelete_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("delete from userfile where username='" + txtUserName.Text + "' ", con);
con.Open();
cmd.ExecuteScalar();
displaygrid();
BindDropDownListData();
}
protected void btnClear_Click(object sender, EventArgs e)
{
txtConfirmationNumber.Text = "";
txtUserName.Text = "";
txtConfirmPassword.Text = "";
txtPassword.Text = "";
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox1.Checked == true)
{
foreach (ListItem checkboxitems in CheckBoxList1.Items)
{
checkboxitems.Selected = true;
}
}
else if (CheckBox1.Checked == false)
{
foreach (ListItem listItem in CheckBoxList1.Items)
{
listItem.Selected = false;
}
}
}
}
THis is actually more accurate to get the checked values for an input field with runat="server".
string isAppleChecked = apple.Attributes["checked"] != null && apple.Attributes["checked"] == "checked" ? "{true}" : "{false}";
精彩评论