在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(); //一定要加上,否则无效。
}
}
}
}
但接下来的问题又来了,用了这种方法要取值各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));
}
}
}
}