C# RadGridEditForm中的复选框CheckedChange事件
我有一个RadGridEditForm模板,其中有复选框和RadComboBox 所以它包含两个按钮 第一次添加新记录(即GridEditFormInsertItem) 第二个-编辑(编辑现有记录。) 我想在选中复选框**时禁用RadComboBox**** 我曾经用“添加新记录”来编写代码,但当我点击**编辑按钮时,它的错误显示为-C# RadGridEditForm中的复选框CheckedChange事件,c#,asp.net,telerik,telerik-grid,C#,Asp.net,Telerik,Telerik Grid,我有一个RadGridEditForm模板,其中有复选框和RadComboBox 所以它包含两个按钮 第一次添加新记录(即GridEditFormInsertItem) 第二个-编辑(编辑现有记录。) 我想在选中复选框**时禁用RadComboBox**** 我曾经用“添加新记录”来编写代码,但当我点击**编辑按钮时,它的错误显示为- Unable to cast object of type 'Telerik.Web.UI.GridEditFormItem' to type 'Telerik.
Unable to cast object of type 'Telerik.Web.UI.GridEditFormItem' to type 'Telerik.Web.UI.GridEditFormInsertItem'.
我的checkedChanged事件代码为
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;
}
}
请告诉我哪里出错了。
提前谢谢。试试这个
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;
}
只需将用作
运算符,因为它不会引发任何无效强制转换的错误。它只需返回null
,如果chkHalfDay.NamingContainer
不是GridEditFormInsertItem
则可以在下一行中检查它,然后将其强制转换到编辑操作时的GridEditFormItem