CHeckBox后台版的连级选择(后期添加功能注释,现在直接上代码)

/// <summary>
    /// 前台出发事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GetCheckBox_Click(object sender, EventArgs e)
    {
        CheckBox chkk = (CheckBox)sender;
        DataControlFieldCell dcf = (DataControlFieldCell)chkk.Parent;
        GridViewRow gvr = (GridViewRow)dcf.Parent;
        int intgvr = gvr.RowIndex;
        GetPreCHE(chkk, intgvr);
        GetSonCHE(chkk, intgvr);
    }
    #region
    //    if (chkk.Checked)
    //    {
    //        int i = gvr.RowIndex;
    //        CheckBox chk = (CheckBox)gvlist.Rows[i].FindControl("CheckBox");
    //        ITEMCODE = gvlist.Rows[i].Cells[2].Text.ToString();
    //        for (int j = 0; j < (ITEMCODE.Length % 4); j++)
    //        {
    //            string itemcode = ITEMCODE.Substring(0, ITEMCODE.Length - j * 4);
    //            for (int z = 0; z < gvlist.Rows.Count; z++)
    //            {
    //                if (gvlist.Rows[z].Cells[2].Text.ToString() == itemcode)
    //                {
    //                    CheckBox chkz = (CheckBox)gvlist.Rows[z].FindControl("CheckBox");
    //                    chkz.Checked = true;
    //                }
    //            }

    //            for (int z = 0; z < gvlist.Rows.Count; z++)
    //            {
    //                if (gvlist.Rows[z].Cells[2].Text.ToString().Length > ITEMCODE.Length)
    //                {
    //                    if (gvlist.Rows[z].Cells[2].Text.ToString().Substring(0, ITEMCODE.Length) == ITEMCODE)
    //                    {
    //                        CheckBox chkz = (CheckBox)gvlist.Rows[z].FindControl("CheckBox");
    //                        chkz.Checked = true;
    //                    }

    //                }

    //            }

    //        }
    //    }
    //   
    //    else
    //    {
    //        int i = gvr.RowIndex;
    //        CheckBox chk = (CheckBox)gvlist.Rows[i].FindControl("CheckBox");
    //        ITEMCODE = gvlist.Rows[i].Cells[2].Text.ToString();
    //        for (int z = 0; z < gvlist.Rows.Count; z++)
    //        {
    //            if (gvlist.Rows[z].Cells[2].Text.ToString().Length > ITEMCODE.Length)
    //            {
    //                if (gvlist.Rows[z].Cells[2].Text.ToString().Substring(0, ITEMCODE.Length) == ITEMCODE)
    //                {
    //                    CheckBox chkz = (CheckBox)gvlist.Rows[z].FindControl("CheckBox");
    //                    chkz.Checked = false;
    //                }

    //            }

    //        }
    //        for (int z = 0; z < gvlist.Rows.Count; z++)
    //        {
    //            int j = 1;

    //            if (gvlist.Rows[z].Cells[2].Text.ToString().Length == ITEMCODE.Length)
    //            {
    //                if (gvlist.Rows[z].Cells[2].Text.ToString().Substring(0, ITEMCODE.Length - 1) == ITEMCODE.Substring(0, ITEMCODE.Length))
    //                {
    //                    j++;
    //                }

    //            }
    //            if (j == 1)
    //            {
    //                for (int b = 0; b < gvlist.Rows.Count; b++)
    //                {
    //                    if (gvlist.Rows[b].Cells[2].Text.ToString().Substring(0, gvlist.Rows[b].Cells[2].Text.ToString().Length - 4) == ITEMCODE.Substring(0, ITEMCODE.Length - 4))
    //                    {
    //                        CheckBox chkz = (CheckBox)gvlist.Rows[z].FindControl("CheckBox");
    //                        chkz.Checked = false;
    //                    }
    //                }
    //            }
    //        }
    //    }

    //}
    #endregion
    /// <summary>
    /// 给父级添加或选择一下
    /// </summary>
    /// <param name="chkk"></param>
    /// <param name="gvr"></param>
    private void GetPreCHE(CheckBox chkk, int intgvr)
    {
        string ITEMCODE = string.Empty;
        int i = intgvr;
        CheckBox chk = (CheckBox)gvlist.Rows[i].FindControl("CheckBox");
        ITEMCODE = gvlist.Rows[i].Cells[2].Text.ToString();
        if (chkk.Checked)
        {

            for (int j = 0; j < gvlist.Rows.Count; j++)
            {
                string itemcode = ITEMCODE.Substring(0, ITEMCODE.Length - 4);
                for (int z = 0; z < gvlist.Rows.Count; z++)
                {
                    if (gvlist.Rows[z].Cells[2].Text.ToString() == itemcode)
                    {
                        CheckBox chkz = (CheckBox)gvlist.Rows[z].FindControl("CheckBox");
                        chkz.Checked = true;
                        GetPreCHE(chkk, z);
                    }
                }
            }
        }
        else
        {
            int j = 1;

            for (int z = 0; z < gvlist.Rows.Count; z++)
            {

                if (gvlist.Rows[z].Cells[2].Text.ToString().Substring(0, gvlist.Rows[z].Cells[2].Text.Length - 1) == ITEMCODE.Substring(0, ITEMCODE.Length - 1))
                {
                    if (gvlist.Rows[z].Cells[2].Text.ToString().Substring(0, ITEMCODE.Length - 1) == ITEMCODE.Substring(0, ITEMCODE.Length - 1))
                    {
                        CheckBox chkz = (CheckBox)gvlist.Rows[z].FindControl("CheckBox");
                        if (chkz.Checked == true)
                        {
                            j++;
                        }

                    }
                }
            }
            //没有跟自己同一父级的同级
            if (j == 1)
            {


                for (int b = 0; b < gvlist.Rows.Count; b++)
                {
                    if (gvlist.Rows[b].Cells[2].Text.ToString() == ITEMCODE.Substring(0, ITEMCODE.Length - 4))
                    {

                        CheckBox chkz = (CheckBox)gvlist.Rows[b].FindControl("CheckBox");
                        chkz.Checked = false;
                        GetPreCHE(chkk, b);

                    }
                }
            }
        }
    }
    /// <summary>
    /// 给子集添加或选择一下
    /// </summary>
    /// <param name="chkk"></param>
    /// <param name="gvr"></param>
    private void GetSonCHE(CheckBox chkk, int intgvr)
    {
        string ITEMCODE = string.Empty;
        int i = intgvr;
        CheckBox chk = (CheckBox)gvlist.Rows[i].FindControl("CheckBox");
        ITEMCODE = gvlist.Rows[i].Cells[2].Text.ToString();
        if (chkk.Checked)
        {

            for (int z = 0; z < gvlist.Rows.Count; z++)
            {
                if (gvlist.Rows[z].Cells[2].Text.ToString().Length > ITEMCODE.Length)
                {
                    if (gvlist.Rows[z].Cells[2].Text.ToString().Substring(0, ITEMCODE.Length) == ITEMCODE)
                    {
                        CheckBox chkz = (CheckBox)gvlist.Rows[z].FindControl("CheckBox");
                        chkz.Checked = true;

                        GetSonCHE(chkk, z);
                    }
                }

            }
        }
        else
        {
            int j = 1;
            //判断是否有子集
            for (int z = 0; z < gvlist.Rows.Count; z++)
            {

                if (gvlist.Rows[z].Cells[2].Text.ToString().Length == ITEMCODE.Length + 4)
                {
                    if (gvlist.Rows[z].Cells[2].Text.ToString().Substring(0, gvlist.Rows[z].Cells[2].Text.Length - 4) == ITEMCODE)
                    {
                        CheckBox chkz = (CheckBox)gvlist.Rows[z].FindControl("CheckBox");
                        chkz.Checked = false;
                        GetSonCHE(chkk, z);
                    }
                }
            }
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值