3

プログラムでチェックボックスを追加したグリッドビューがあります。foreachループ内にチェックボックスを作成するときは、次のようにして、チェックしたときにイベントをトリガーするようにします。

            cbGV = new CheckBox();
            cbGV.ID = "cbGV";
            cbGV.AutoPostBack = true;
            cbGV.CheckedChanged += new EventHandler(this.cbGV_CheckedChanged);

基本的に、イベントをトリガーしたいときは、以下のようにします。

    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?

        }

事前にご回答いただきありがとうございます。

4

3 に答える 3

3

まず、CheckBoxesこれがチェックされているときだけ他のチェックを外す必要があります (それが必要な場合) 。チェックを外したCheckBoxときではありません。

次に、演算子を使用==して、このチェックボックスを他のチェックボックスと比較できます。

CheckBox activeCheckBox = sender as CheckBox;
if(activeCheckBox.Checked)
{
    foreach (GridViewRow gvr in GridView1.Rows)
    {
        CheckBox checkBox = ((CheckBox)gvr.FindControl("cbGV"));
        checkBox.Checked = checkBox == activeCheckBox;
    }
}
于 2012-11-07T10:02:57.987 に答える
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;
    }
}
于 2013-09-20T14:49:14.067 に答える
0

私はそれを試しませんでしたが、これを行う1つの方法は次のとおりです。

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?

        }
于 2012-11-07T10:05:53.820 に答える