Here's my setup: I have a MasterPage that has a webform with a RadGrid in it. That RadGrid has one nested table. I'm trying to insert a new item into that nested table using a WebUserControl (ascx) page as the EditForm for the nested Grid.
I've read the help at: http://www.telerik.com/help/aspnet-ajax/grdextractprimarykeyforparentiteminhierarchyonupdateinsert.html
My grid and nested grid are setup pretty much identically to what is there, except that I'm using a MasterPage and usercontrol for the form and I have a dataobject for the datasource.
When updating the nested grid with the usercontrol, the world is a wonderful place and things work just fine. However, when that same form is used for inserting, the binding values go away. In the customers and orders example, the customerID is not set to the default for the primarykey of the parent item.
I tried the code from http://www.telerik.com/help/aspnet-ajax/grdcustomeditforms.html for accessing the grid edited item from the usercontrol.
It generates a compiler error (cannot implictly convert system.web.ui.control to telerik.web.ui.GridEditableItem. An explict conversion exists). When I explictly cast, editedItem.KeyValues is null. I think this may be something funky with the masterpages.
If parentID is an int32, I can use the following code in the main aspx code behind to get the appropriate value:
But I can not for the life of me access that value from the ascx page. I'm also assuming this is because of some masterpage stuff that I haven't figured out yet.
So, How can I reference that DataKeyValue from the WebUserControl for an insert function? I need to set the default value for the customerID. What is the best way to accomplish this?
5 Answers, 1 is accepted
Hello David ,
Here's an example of how to access the parent item of a child record while inserting and set the property of the user control with a field value of the parent item:
I have added the ItemDataBound event, and verified that it fires. I also verify that the if statement containing the three conditions fires.
On stepping through things, I run into an error. insertItem.ParentItem is null. insertItem has the value of Telerik.Web.UI.GridEditFormInsertItem. But the value for ParentItem is null. Why would this be the case? I assume that I have something configured wrong.
GridDataItem parentItem = (GridDataItem)insertItem.ParentItem;
GridDataItem parentItem = (GridDataItem)e.Item.OwnerTableView.ParentItem;
I get the correct value. However, when I try to use that value in the usercontrol page on the button event for save, I have a value of 0. It's like the value is lost when the RadGrid loads the usercontrol.
Thanks for the help. Maybe I should be clearer about what is going on. I handle the insert event from the button click event of the user control. So when the button clicks it fires the on_click event in the user control where I attach values to my object and save it. I need the CustomerID available in that event. Or I need to understand the model for handling the insert event in the page with the grid (aspx) with access to the new values from the user control form.
Here's the code to help:
I've changed the names of things to make the example easier. I think I got it all Here is the page Customers.aspx:
Here is the code behind for that page:
When I step through this, usercntrl.parentID will be the correct value.
So then I have a ascx page called OrderEdit with a form and a button. I didn't include it as it's just a form and a button. If you think that's the problem let me know and I'll post it. Here is the code behind for that page:
For your scenario, the approach provided by Telerik in the help document would be an appropriate solution, just that you would have to modify it slightly as shown below:
Sorry I didn't get around to testing this solution until this AM.
The syntax on the last line of the page_load was giving me errors about using index on the method. So I changed that line to be the following:
parentID = (Convert.ToInt32(parentItem.GetDataKeyValue("PrimaryKeyField").ToString()));
And everything is working well. Thanks for your time and help.