This is a migrated thread and some comments may be shown as answers.

Radgrid - Problem on Add New Record

3 Answers 219 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Brad
Top achievements
Rank 1
Brad asked on 08 Jul 2011, 06:12 AM
I have a RadGrid setup so that double clicking a row opens a form under the row to allow editing. The form itself lives in a web user control.

On this form one of the fields is a CheckBox. It looks like this.

<td><asp:CheckBox ID="chkIsActive" runat="server" Checked='<%# DataBinder.Eval( Container, "DataItem.con_Is_Active") %>' /></td>
 
The code in the 'Checked=' bit sees that on edit the Checked property gets set to True or False. Perfect.

However, when I hit the Add New Record button, this line gets a problem.

Specified cast is not valid.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error:

Line 24:          <tr>
Line 25:          <td align="right">Active:</td>
Line 26:          <td><asp:CheckBox ID="chkIsActive" runat="server" Checked='<%# DataBinder.Eval( Container, "DataItem.con_Is_Active") %>' /></td>
Line 27:          <td></td>
Line 28:         <td></td>

Source File: c:\Dev\RadGrid\RadGrid\Controls\ContactDetails.ascx    Line: 26



It's pretty easy to understand why.  DataItem.con_Is_Active has no value on an Add New Record insert.

My question is, were do I intercept this to prevent this error on Adding a new Record?

I've looked for but have been unable to find the InitInsertButtons click event if any such event exists..

I'm sure this is simple I just don't know where to put such a check.  I suppose what I'd like to do is set the  DataItem.con_Is_Active property some default value (like 'False') before the it gets to this point.

3 Answers, 1 is accepted

Sort by
0
Sebastian
Telerik team
answered on 08 Jul 2011, 08:37 AM
Hi Brad,

Your assumption is right on target. You should intercept the init insert command and specify a default value (true or false) for the checkbox when this command is triggered. See the bottom section of this documentation topic for more details.

Best regards,
Sebastian
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Brad
Top achievements
Rank 1
answered on 11 Jul 2011, 12:11 AM

This worked for Inserting but it messed up my Editing of rows.

This line

<td><asp:CheckBox ID="chkIsActive" runat="server" Checked='<%# DataBinder.Eval( Container, "DataItem.con_Is_Active") %>' /></td>

Still bombed out on Inserting as the DataItem value is not set on adding a new item.

So my only option was to remove the Checked property.

<td><asp:CheckBox ID="chkIsActive" runat="server"/></td>

Of course now the Editing of a row does not work.

The only option then it seems is to populate this checkbox when editing as well.

So I have modified my ItemCommand method to look after updating a row as well.

protected void rgContacts_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == Telerik.Web.UI.RadGrid.InitInsertCommandName)
            {
                // etc etc
            }
            else if (e.CommandName == Telerik.Web.UI.RadGrid.EditCommandName)
            {

My problem now it how to I find the User control for an Update?

I have tried all sorts of thing with e.Item but I just can't work it out.

This my last failed attempt.. Seems logical to me but does not work.

GridDataItem editItem = (GridDataItem)e.Item;
String MyUserControlId = GridEditFormItem.EditFormUserControlID;
UserControl MyUserControl = editItem.FindControl(MyUserControlId) as UserControl;
CheckBox chkIsActive = MyUserControl.FindControl("chkIsActive") as CheckBox;


After this line

UserControl MyUserControl = editItem.FindControl(MyUserControlId) as UserControl;

MyUserControl is null.

Almost there. Any ideas please.


0
Brad
Top achievements
Rank 1
answered on 12 Jul 2011, 02:20 AM
I found the solution to this.

Word is that the Edit usercontrol does not exist at this stage of the grid's lifecycle. The correct place to access it is in the ItemDataBound event.

The solution is explained nicely here.

http://www.telerik.com/community/forums/aspnet/grid/find-control-on-item-command-in-radgrid.aspx
Tags
General Discussions
Asked by
Brad
Top achievements
Rank 1
Answers by
Sebastian
Telerik team
Brad
Top achievements
Rank 1
Share this question
or