Teju MB
Teju MB

Reputation: 1383

CheckBox CheckedChange event in RadGridEditForm

enter image description hereI have a RadGridEditForm Template, Where i have checkBox and RadComboBox.

So it contains 2 buttons

1st-Add New Record (Which is GridEditFormInsertItem)

2nd- Edit(To edit the existing record.)

I want to Disable the RadComboBox when the checkbox is **CHECKED**** I used to folowing code its working fine with 'Add New Record' but not when i click on **Edit button its showing error as-

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

My code for checkedChanged Event is

    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;
        }
}

Please Suggest where i went wrong. Thanks in Advance.

Upvotes: 0

Views: 1234

Answers (1)

yogi
yogi

Reputation: 19591

Try this

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;
}

Just use as operator because it won't raise any error of invalid cast it'll simply return null which you can check in next line if chkHalfDay.NamingContainer isn't GridEditFormInsertItem then cast it to GridEditFormItem which is at the time of Edit operation.

Upvotes: 1

Related Questions