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

[Solved] UserControl as EditForm for RadGrid

1 Answer 434 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Andrei
Top achievements
Rank 1
Andrei asked on 01 Oct 2009, 10:30 AM
Hello there,
I have a UserControl that i am trying to use as edit form for a RadGrid. The UserControl has some controls inside that perform postbacks (such as a RadComboBox changing the value of a RadNumericTextBox on SelectedIndexChanged), however I ajaxified this using a RadAjaxManagerProxy and adding the combobox as initiator and the same combobox and the textbox as updated controls.

In the NeedDataSource handler of the grid, i set the DataSource property with a collection of objects and in the ItemDataBound handler for the grid, if the item is in edit mode, i set some properties of the UserControl, including the one which stores the id of the object to be loaded.

This is the code for the two handlers:
protected void grdRelated_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
    { 
        grdRelated.DataSource = GetGridDataSource(); 
    } 
 
protected void grdRelated_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridEditFormItem &&  e.Item.IsInEditMode) 
        { 
            IAssignControl ctrl = e.Item.FindControl(GridEditFormItem.EditFormUserControlID) as IAssignControl; 
            if (ctrl != null
            { 
                AssignedProduct assignedProduct = (e.Item as GridEditFormItem).DataItem as AssignedProduct; 
                ctrl.EntityType = "Opportunity"
                ctrl.BOId = "86cc5561-d769-4b77-b51e-386e989de0ae"
                ctrl.InAssignMode = false
                ctrl.AssignedProductID = assignedProduct.UniqueID; 
                ctrl.Refresh(); 
            } 
        } 
    } 

ctrl.Refresh() calls a method inside the UserControl which redraws the inner controls, as they are dynamically added to the UserControl.

When the edit button inside the grid is pressed, putting the row in edit mode, everything works ok and the UserControl is loaded properly. However, let's say i change the selection of a combobox inside the UserControl. As i said, this needs to update a textbox in the same user control according to the new selection. Though the combobox would normally trigger a postback and refresh the whole page, textbox included, as this is ajaxified using the RadAjaxManagerProxy defined inside the UserControl, i would expect only the textbox to be updated and no action performed on the other inner controls or the radgrid having this UserControl as edit form.

What actually happens is that somehow, the NeedDataSource event is fired again, reloading the grid data source, which then triggers a Rebind on the grid, which then goes into ItemDataBound, reassigns the UserControl properties and triggers the ctrl.Refresh() which redraws the inner controls. At this point, the inner controls are re-created according to the initial data, while the ViewState contains the modified data, leading to a mismatch between the saved state and the current state. Bingo, corrupted ViewState error.

What i don't understand is why an ajax request inside the UserControl leads to the containing grid firing its NeedDataSource event. The way I see it (please correct me if i am wrong with this), the ajax request should only update the controls specified in the RadAjaxManagerProxy settings, not the whole grid. Have i made any mistake in approaching this? If so please shed some light on this matter as I have no idea how to get past this.

While browsing the other forum threads, i came across THIS thread, which seems related, though i cannot say for sure it's the same problem i'm experiencing.

1 Answer, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 05 Oct 2009, 03:28 PM
Hi Andrei,

The link you refer to may not necessarily be related to your case. On the other hand, if you are AJAX-ifying the entire RadGrid, note that you get an update panel inside an update panel (1 update panel from the AJAX proxy settings for the user control inside the update panel for AJAX-ified RadGrid). This scenario is not recommended with the MS AJAX framework, as things may get messed up following the question: "Which update panel should callback - the inner or the outer". This might be one reason for the behavior you are getting.

If this is the case, try disabling AJAX for RadGrid and test the scenario with a single AJAX panel for the user control only.

P.S. I am always referring to AJAX panels, as RadAjaxManager (and its proxy), effectively create panels around the updated controls.

Kind regards,
Veli
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Andrei
Top achievements
Rank 1
Answers by
Veli
Telerik team
Share this question
or