Check if 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 am sending booleans to the SQL database. The problem is only untested . I mean, when you check some checkboxes, it sends it as true (and rightly so), but when I remove them, it remains the same (true).

Addition: Why too little? request here ... 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 data type is bit .... I also tried with a string ... but without success

PS - I also tried with Request.Form ["apple"], and unchecking it worked ... but, unfortunately, the check is not ... when I check the box, it gives me an error:

 Conversion failed when converting the varchar value 'on' to data type bit. 

Is anyone

+6
source share
6 answers

So, after a long time of combinations and other things, it worked ... Without any javascripts and hidden fields ... that .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; 
+6
source share

Unchecked flags are not sent when the form is submitted. You will have to write a workaround.

One of the methods is to have a hidden field that is populated using javascript of this flag.

+1
source share

First, I would order your code at the beginning if the if statements are not needed:

 if (Page.IsPostback) { bool appleSelected = apple.Checked; bool pizzaSelected = pizza.Checked; bool orangeSelected = orange.Checked; } 

Have you tried using the CheckBox class instead of typing?

 <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" /> 
0
source share

It should work because the data type is bit ... at least when you pass the bool to a stored procedure.

Since there is an SQL update statement in your code, try converting bool to 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); 
0
source share
 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; } } } } 
0
source share

It is indeed more accurate to get verified values ​​for an input field using runat = "server".

string isAppleChecked = apple.Attributes ["checked"]! = null & & apple.Attributes ["checked"] == "checked"? "{true}": "{false}";

0
source share

All Articles