RadGridEditForm中的CheckBox CheckedChange事件

时间:2012-10-09 06:34:32

标签: c# asp.net telerik telerik-grid

enter image description here我有一个RadGridEditForm模板,我有checkBox和RadComboBox。

所以它包含2个按钮

1st-Add New Record(GridEditFormInsertItem)

2nd-编辑(编辑现有记录。)

我想在复选框为** CHECKED ****时禁用RadComboBox 我过去常常使用'添加新记录'来使其工作正常但不是当我点击**编辑按钮时显示错误为 -

Unable to cast object of type 'Telerik.Web.UI.GridEditFormItem' to type 'Telerik.Web.UI.GridEditFormInsertItem'.

checkChanged事件的代码是

    protected void chkEHalfDay_CheckedChanged(object sender, EventArgs e)
{
    CheckBox chkHalfDay = (CheckBox)sender;
    GridEditFormInsertItem item = (GridEditFormInsertItem)chkHalfDay.NamingContainer;

        if (chkHalfDay.Checked == false)
        {
            ((RadComboBox)item.FindControl("rcbHalfDayType")).Enabled = false;
        }
        else
        {
            ((RadComboBox)item.FindControl("rcbHalfDayType")).Enabled = true;
        }
}

请建议我哪里出错了。 在此先感谢。

1 个答案:

答案 0 :(得分:1)

试试这个

protected void chkEHalfDay_CheckedChanged(object sender, EventArgs e)
{
   CheckBox chkHalfDay = (CheckBox)sender;
   //GridEditFormInsertItem item = (GridEditFormInsertItem)chkHalfDay.NamingContainer;
   GridEditFormInsertItem item = chkHalfDay.NamingContainer as GridEditFormInsertItem;
   if(item == null)
     item = hkHalfDay.NamingContainer as GridEditFormItem;

  if (chkHalfDay.Checked == false)
     ((RadComboBox)item.FindControl("rcbHalfDayType")).Enabled = false;
  else
      ((RadComboBox)item.FindControl("rcbHalfDayType")).Enabled = true;
}

只需使用as运算符,因为它不会引发任何无效投射错误,只会返回null,如果chkHalfDay.NamingContainer不是{{}},您可以在下一行查看1}}然后将其转换为编辑操作时的GridEditFormInsertItem