Ext.net 动态绑定CheckBoxGroup并取值

在PageLoad中直接加载绑定CheckBoxGroup的方法,应该大家都会,而且取值也很简单,但问题是这种代码只能在页面加载时即绑定,当需要在页面上根据条件再来重新绑定的时候就生成不了,只能postback强行刷新页面。为什么gridPanel等控件可以随时绑定,随时取值,为什么CheckBoxGroup就不行,花了一个上午才最终在Ext官网上找到说法:CheckBoxGroup很遗憾的不支持,但给了解决方法,我的实例代码如下:

//[DirectMethod]
    protected void  AddSysDept(object sender,DirectEventArgs e)
    {
        if (NodeID.Contains(",") | NodeID == "")
        {
            X.Msg.Alert("系统提示", "系统错误,请与管理员联系!").Show();

        }
        else
        {
            int AreaID = Int32.Parse(NodeID);
            BLLConfiguration Conf = new BLLConfiguration();
            int CentralFlag = 0;
            DataTable dt = Conf.Dt_SysDeptCanSetByAreaID(AreaID,out CentralFlag);
            if (CentralFlag == 1)
            {
                wdDeptNew.Show();

            }
            else
            {
                if (dt.Rows.Count == 0)
                {
                    X.Msg.Alert("系统提示", "没有可增加的系统别").Show();

                }
                else
                {
                    wdDeptAdd.Show();
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        Ext.Net.Checkbox ckb = new Checkbox();
                        ckb.ID = dt.Rows[i]["DeptID"].ToString();
                        ckb.BoxLabel = dt.Rows[i]["SysDept"].ToString();
                        cbgDepts.Items.Add(ckb);
                        ckb.Listeners.Check.Handler = "Ext.net.DirectMethods.GetValue('" + ckb.ID + "')";

                    }
                    cbgDepts.Render(); //一定要加上,否则无效。
                }
            }
        }
    }


加上 cbgDepts.Render(); 后即可以无需在Pageload中加载方法,也可通过点击Button等控件来随意绑定生成。


但接下来的问题又来了,用了这种方法要取值各CheckBox的选值,用之前的遍历取CheckBoxGroup的CheckItems的方法是取不到的值的。但如果在之前的绑定生成的代码中为CheckBox赋上监听事件后 ckb.Listeners.Check.Handler = "Ext.net.DirectMethods.GetValue('" + ckb.ID + "')";  还是可以曲线取到。
此时取值要略显麻烦,但也是无奈之举,如有更好方式还请告知,附上取值代码:

 [DirectMethod]
    public void GetValue(string ID)
    {
        string DeptID="(" + ID + ")";
        if (hdDept.Text.Contains(DeptID))
        {
            hdDept.Text= hdDept.Text.Replace(DeptID, "");
        }
        else
        {
            if (hdDept.Text == "")
            {
                hdDept.Text = DeptID;

            }
            else
            {
                hdDept.Text = hdDept.Text + "," + DeptID;
            }
        }
    }

 [DirectMethod]
    public void NewDeptSubmit()
    {
            string DeptIDs = hdDept.Text;
            if (DeptIDs.Replace(",","") != "")
            {
                BLLConfiguration Conf = new BLLConfiguration();
                string[] IDArray = DeptIDs.Split(new char[] { ',' });
                List<string> array = new List<string>(IDArray);
                int AreaID = Int32.Parse(NodeID);
                string SysID;
                for (int i = 0; i < array.Count; i++)
                {
                    if (array[i].Contains("("))
                    {
                        SysID = array[i].Replace("(", "");
                        SysID = SysID.Replace(")", "");
                        Conf.SysDeptAdd(AreaID, Int32.Parse(SysID));
                               
                    }
                }
                
            }
    }



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值