I have a gridview where i have added checkboxes programmatically. i do as following when creating checkboxes inside a foreach loop, so that they trigger an event when checked,
我有一个gridview,其中我以编程方式添加了复选框。当在foreach循环中创建复选框时,我这样做,以便在选中时触发事件,
cbGV = new CheckBox();
cbGV.ID = "cbGV";
cbGV.AutoPostBack = true;
cbGV.CheckedChanged += new EventHandler(this.cbGV_CheckedChanged);
So basically when i want the event to be triggered, i have the following below,
所以基本上当我想要事件被触发时,我有以下几点,
protected void cbGV_CheckedChanged(object sender, EventArgs e)
{
//gets the current checked checkbox.
CheckBox activeCheckBox = sender as CheckBox;
foreach (GridViewRow gvr in GridView1.Rows)
{
//this code is for finding the checkboxes in the gridview.
CheckBox checkBox = ((CheckBox)gvr.FindControl("cbGV"));
//so basically, right here i'm confused on how i should compare the if/else logic, how i should compare and disable every other checkbox if the current checkbox is checked. Any ideas gues?
}
thanks for your answer in advance.
谢谢你事先的答复。
3 个解决方案
#1
3
First, you should only uncheck the other CheckBoxes
(if that is what you want) when this CheckBox
is checked and not when it was unchecked.
首先,您应该只在选中该复选框时取消选中其他复选框(如果这是您想要的),而不是在选中该复选框时取消选中。
Second, you can use ==
operator to compare this checkbox with the others:
其次,可以使用== =运算符将该复选框与其他复选框进行比较:
CheckBox activeCheckBox = sender as CheckBox;
if(activeCheckBox.Checked)
{
foreach (GridViewRow gvr in GridView1.Rows)
{
CheckBox checkBox = ((CheckBox)gvr.FindControl("cbGV"));
checkBox.Checked = checkBox == activeCheckBox;
}
}
#2
0
I did not try it, but here is one way to do it:
我没有尝试,但有一种方法:
protected void cbGV_CheckedChanged(object sender, EventArgs e)
{
//gets the current checked checkbox.
CheckBox activeCheckBox = sender as CheckBox;
BOOL flag = false;
CheckBox selectedCheckBox;
foreach (GridViewRow gvr in GridView1.Rows)
{
//this code is for finding the checkboxes in the gridview.
CheckBox checkBox = ((CheckBox)gvr.FindControl("cbGV"));
if (checkBox.Checked==true && flag==false)
{
flag = true;
selectedCheckBox = checkBox;
}
else
{
if (checkBox != selectedCheckBox)
{
checkBox.Enabled = false;
checkBox.Checked = false;
}
}
//so basically, right here i'm confused on how i should compare the if/else logic, how i should compare and disable every other checkbox if the current checkbox is checked. Any ideas gues?
}
#3
0
CheckBox activeCheckBox = sender as CheckBox;
// to uncheck all check box
foreach (GridViewRow rw in GrdProc.Rows)
{
CheckBox chkBx = (CheckBox)rw.FindControl("ChkCust");
if (chkBx != activeCheckBox )
{
chkBx.Checked = false;
}
}
#1
3
First, you should only uncheck the other CheckBoxes
(if that is what you want) when this CheckBox
is checked and not when it was unchecked.
首先,您应该只在选中该复选框时取消选中其他复选框(如果这是您想要的),而不是在选中该复选框时取消选中。
Second, you can use ==
operator to compare this checkbox with the others:
其次,可以使用== =运算符将该复选框与其他复选框进行比较:
CheckBox activeCheckBox = sender as CheckBox;
if(activeCheckBox.Checked)
{
foreach (GridViewRow gvr in GridView1.Rows)
{
CheckBox checkBox = ((CheckBox)gvr.FindControl("cbGV"));
checkBox.Checked = checkBox == activeCheckBox;
}
}
#2
0
I did not try it, but here is one way to do it:
我没有尝试,但有一种方法:
protected void cbGV_CheckedChanged(object sender, EventArgs e)
{
//gets the current checked checkbox.
CheckBox activeCheckBox = sender as CheckBox;
BOOL flag = false;
CheckBox selectedCheckBox;
foreach (GridViewRow gvr in GridView1.Rows)
{
//this code is for finding the checkboxes in the gridview.
CheckBox checkBox = ((CheckBox)gvr.FindControl("cbGV"));
if (checkBox.Checked==true && flag==false)
{
flag = true;
selectedCheckBox = checkBox;
}
else
{
if (checkBox != selectedCheckBox)
{
checkBox.Enabled = false;
checkBox.Checked = false;
}
}
//so basically, right here i'm confused on how i should compare the if/else logic, how i should compare and disable every other checkbox if the current checkbox is checked. Any ideas gues?
}
#3
0
CheckBox activeCheckBox = sender as CheckBox;
// to uncheck all check box
foreach (GridViewRow rw in GrdProc.Rows)
{
CheckBox chkBx = (CheckBox)rw.FindControl("ChkCust");
if (chkBx != activeCheckBox )
{
chkBx.Checked = false;
}
}