[英]Check whether the checkbox is checked or not - ASP.NET
我有以下代碼:
(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>
現在,我將布爾變量發送到SQL數據庫。 問題只出現在未經檢查的方框中。 我的意思是,當你檢查一些復選框時,它會將其發送為真(這是正確的)但是當我取消選中它時它保持不變(真實)。
另外:為什么太少? 這是一個查詢...這里沒什么特別的
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...
數據類型是位....我也嘗試使用字符串...但沒有成功
PS - 我也嘗試過Request.Form [“apple”],取消選中工作......但遺憾的是檢查沒有...當我選中復選框時它會拋出一個錯誤:
Conversion failed when converting the varchar value 'on' to data type bit.
有人?
所以經過很長一段時間的組合和其他東西它工作...沒有任何javascripts和隱藏的字段...這是.cs代碼
bool apple2 = (Request.Form["apple"] == "on") ? true : false;
bool orange2 = (Request.Form["orange"] == "on") ? true : false;
bool pizza2 = (Request.Form["pizza"] == "on") ? true : false;
發布表單時,不會提交未選中的復選框。 你必須寫一個解決方法。
一種方法是通過復選框的javascript填充隱藏字段。
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;
}
}
}
}
實際上,使用runat =“server”獲取輸入字段的選中值更准確。
string isAppleChecked = apple.Attributes [“checked”]!= null && apple.Attributes [“checked”] ==“checked”? “{真假}”;
首先,我會在開頭整理你的代碼,if語句是不必要的:
if (Page.IsPostback)
{
bool appleSelected = apple.Checked;
bool pizzaSelected = pizza.Checked;
bool orangeSelected = orange.Checked;
}
您是否嘗試過使用CheckBox類而不是輸入?
<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" />
它應該工作,因為數據類型是位...至少當你將bool傳遞給存儲過程時。
由於您的代碼中包含SQL更新語句,請嘗試將bool轉換為0或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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.