如何获取多个checkbox的值判断其是否被选中,然后传入数据库?
/// <summary>
/// 复选框
/// </summary>
public void HelpCheck()
{
for (int i = 1; i <=14; i++)
{
check = (CheckBox)this.Controls.Find("checkBox" + i.ToString(), true)[0];
check.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
}
}
/// <summary>
///多个复选框的绑定事件选择情况
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = sender as CheckBox;
if (chk.Checked==true)
{
str += chk.Text + ",";
}
}
可以获取数据,但是checkBox1_CheckedChanged事件不合适,该怎么写呢?
checkbox C# --------------------编程问答-------------------- 比如某单击事件再统计选择的复选框,之后存数据库,而不是在每个复选框CheckedChanged事件统计, --------------------编程问答-------------------- 定义一个方法:
private string SearchCheckedCheckBox(Control.ControlCollection controlCllection)
{
string text = string.Empty;
foreach (var item in controlCllection)
{
if (item is CheckBox)
{
CheckBox checkBox = item as CheckBox;
if (checkBox.Checked)
{
text += checkBox.Text;
}
}
}
return text;
}
调用(在普通button的click事件中调用):
SearchCheckedCheckBox(panel1.Controls);//假设是panel1里面的Controls--------------------编程问答-------------------- 循环所有CheckBox类型的控件,判断是Check属性是否为True即可
补充:.NET技术 , C#