C# 检查是否选中该复选框-ASP.NET
我有以下代码: (some.aspx.cs) (some.aspx) 数据类型是bit…我也尝试过使用string…但没有成功 另外,我还尝试了Request.Form[“apple”],取消勾选有效…但不幸的是,勾选无效…当我勾选该复选框时,它会抛出一个错误:C# 检查是否选中该复选框-ASP.NET,c#,asp.net,C#,Asp.net,我有以下代码: (some.aspx.cs) (some.aspx) 数据类型是bit…我也尝试过使用string…但没有成功 另外,我还尝试了Request.Form[“apple”],取消勾选有效…但不幸的是,勾选无效…当我勾选该复选框时,它会抛出一个错误: Conversion failed when converting the varchar value 'on' to data type bit. 某人?首先,我会在开始时整理您的代码,如果不需要这些语句: if (Page.Is
Conversion failed when converting the varchar value 'on' to data type bit.
某人?首先,我会在开始时整理您的代码,如果不需要这些语句:
if (Page.IsPostback)
{
bool appleSelected = apple.Checked;
bool pizzaSelected = pizza.Checked;
bool orangeSelected = orange.Checked;
}
您是否尝试过使用类而不是输入
<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" />
发布表单时,不提交未选中的复选框。你必须写一个变通方法
一种方法是通过复选框的javascript填充一个隐藏字段。它应该可以工作,因为数据类型是bit…至少当您将bool传递给存储过程时是这样 由于代码中有SQL update语句,请尝试将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);
所以经过长时间的组合和其他东西,它工作了…没有任何Java脚本和隐藏字段。。。 这是.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;
使用System.Data.SqlClient;
使用系统数据;
公共部分类\u默认值:System.Web.UI.Page
{
SqlConnection con=newsqlconnection(“数据源=RND3”+“\\\”+“SQLEXPRESS;初始目录=SSSolutionFiles;集成安全性=True”);
公共void displaygrid()
{
SqlDataAdapter da=新的SqlDataAdapter(“从用户文件中选择*”,con);
数据集ds=新数据集();
da.填充(ds,“p”);
GridView1.DataSource=ds.Tables[“p”];
GridView1.DataBind();
}
受保护的无效页面加载(对象发送方、事件参数e)
{
//Label1.Text=txtextername.Text+“
”+txtPassword.Text+“
”+txtConfirmPassword.Text+“
”+txtConfirmationNumber.Text;
displaygrid();
如果(!IsPostBack)
BindDropDownListData();
}
public void bindDropDownList数据()
{
//SqlConnection con=newsqlconnection(“数据源=RND3”+“\\\”+“SQLEXPRESS;初始目录=SSSolutionFiles;集成安全性=True”);
//SqlConnection mySqlConnection=newsqlconnection();
{
尝试
{
con.Open();
SqlCommand mySqlCommand=newsqlcommand(“从用户文件中选择用户名”,con);
SqlDataAdapter mySqlDataAdapter=新的SqlDataAdapter(mySqlCommand);
DataSet myDataSet=新数据集();
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();
}
捕获(例外情况除外)
{
Label1.Text=例如消息;
}
最后
{
con.Close();
}
}
}
受保护的void btnsupmit\u单击(对象发送者,事件参数e)
{
SqlCommand cmd=new-SqlCommand(“插入用户文件值(““+txtextername.Text+”、“+txtPassword.Text+”、“+txtConfirmPassword.Text+”、“+txtConfirmPassword.Text+”)、con);
con.Open();
cmd.ExecuteScalar();
displaygrid();
BindDropDownListData();
}
受保护的void b更新\u单击(对象发送方,事件参数e)
{
SqlCommand cmd=new SqlCommand(“更新用户文件集confirmnumber=”+txtConfirmationNumber.Text+”,密码=“+txtPassword.Text+”,confirmpassword=”+txtConfirmPassword.Text+”,其中username=“+txtextername.Text+”,con);
con.Open();
cmd.ExecuteScalar();
displaygrid();
}
受保护的无效BTN删除\单击(对象发送者,事件参数e)
{
SqlCommand cmd=new SqlCommand(“从username='”+txtUserName.Text+'”,con的用户文件中删除);
con.Open();
cmd.ExecuteScalar();
displaygrid();
BindDropDownListData();
}
受保护的无效btnClear\u单击(对象发送方,事件参数e)
{
txtConfirmationNumber.Text=“”;
txtUserName.Text=“”;
txtConfirmPassword.Text=“”;
txtPassword.Text=“”;
}
受保护的无效复选框1\u CheckedChanged(对象发送方,事件参数e)
{
if(CheckBox1.Checked==true)
{
foreach(CheckBoxList1.Items中的ListItem checkboxitems)
{
checkboxitems.Selected=true;
}
}
else if(CheckBox1.Checked==false)
{
foreach(CheckBoxList1.Items中的ListItem ListItem)
{
listItem.Selected=false;
}
}
}
}
使用runat=“server”获取输入字段的检查值实际上更准确
字符串isAppleChecked=apple.Attributes[“checked”]!=无效的
&&apple.Attributes[“checked”]=“checked”?“{true}”:“{false}” 信息太少,无法看到发生了什么。SQL端发生了什么?SQL表中apple&orange的数据类型是什么?这也不起作用。。。我认为checkbox类和input之间没有太大区别。。。另外,其他输入(用户名、通行证等)工作正常……但只有这个复选框不行……天哪……为什么这样奇怪?我相信有更简单的方法可以做到这一点……否则asp不是asp的东西,而是HTML规范的东西。它表示任何没有值的控件都不需要过帐。未选中的复选框没有值。我会按照其他建议尝试使用asp:CheckBoxok…但是asp:CheckBoxok不起作用…在IE中,它更有趣…当我只取消选中一个时,它会将所有的“食物”都变成假的…我已经尝试过这个方法了…我做到了
<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" />
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);
bool apple2 = (Request.Form["apple"] == "on") ? true : false;
bool orange2 = (Request.Form["orange"] == "on") ? true : false;
bool pizza2 = (Request.Form["pizza"] == "on") ? true : false;
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;
}
}
}
}