2014-09-26 87 views
-2
protected void Button1_Click1(object sender, EventArgs e) 
{ 
    try 
    { 
     string str = mas.empdetadd(CheckBox1.Checked.ToString(), CheckBox2.Checked.ToString(), CheckBox3.Checked.ToString(), CheckBox4.Checked.ToString(), txtnom.Text, ddgroup.SelectedItem.Text); 

     if (str == "1") 
     { 
      ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('Employee Details Already Added In this ID ..!!');", true); 
     } 
     else 
     { 
      ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('Employee Details Added Succesfully..!!');", true); 
     } 
    } 
    catch (Exception ex) 
    { 
     Label1.Text = ex.Message; 
    } 
} 

我有4个复选框。目前,当我点击一个复选框时,它将返回值为“true”和“false”。我想将这个真值保存为“1”,将假值保存为“0”到数据库中。我怎样才能做到这一点。此外,如何清除该复选框按钮单击事件后设置复选框值

我的班级

public string empdetadd(string ot, string pf, string esi, string tds) 
{ 
    string qry = "insert into EmpDetails values(+ ot + "','" + pf + "','" + esi + "','" + tds + "')"; 
} 
+1

'INT I = checkbox.Checked? 1:0;'用'I'做什么你想要什么 – Reniuz 2014-09-26 06:12:33

+0

请改善你的问题。它不显示代码将值保存到数据库的位置。只需使用if(CheckBox1.Checked)而不是将其转换为字符串。 'Set Checkbox1.Checked = false'用于清除复选框 – CarbineCoder 2014-09-26 06:16:46

+0

Ot,pf,esi和tds是checkbox1,checkbox2,checkbox3和checkbox4.I不知道如何将其值转换为1和0. – 2014-09-26 06:21:21

回答

2

试试这个:

string str = mas.empdetadd(Convert.ToInt32(CheckBox1.Checked),Convert.ToInt32(CheckBox2.Checked), Convert.ToInt32(CheckBox3.Checked), Convert.ToInt32(CheckBox4.Checked),txtnom.Text, ddgroup.SelectedItem.Text); 
+0

感谢它工作正常.. – 2014-09-26 06:46:02

+0

@Signetsoftwaretrainee永远欢迎:) – 2014-09-26 06:46:38

1

为什么要值存储为1或0。只需使用参数化查询,并设置布尔类型的参数值即'真'或'假'。保持您的数据库列也是布尔类型,并让数据库以它喜欢的方式存储它。

要取消选中表单上的所有复选框,您可以使用此代码。

foreach (void Control_loopVariable in this.Controls) { 
    Control = Control_loopVariable; 
    if ((Control) is CheckBox) { 
     ((CheckBox)Control).Checked = false; 
    } 
} 
+0

先生,我想将值存储为1或0,然后只有当值为1时员工才有资格获得PF,且值为0,则他不符合资格获得pf。 – 2014-09-26 06:27:01

+0

如果您的列只能有两个值,那么最好将它作为布尔值存储。假设1为真,0为假,然后实现任何你想要的逻辑。如果你想要1或0,那么你可以在Reniuz的第一条评论中回复你的问题。 – prem 2014-09-26 06:30:32