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

DataItem lost after postback caused by UserControl

5 Answers 181 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Makoto
Top achievements
Rank 1
Makoto asked on 07 Jul 2012, 06:27 AM
Hello,

I have a RadGrid with EditFormType="WebUserControl". The UserControl has a view model (custom class) and controls on the UserControl binds to the view model. And there are radio buttons on the UserControl that causes postback.

I'm setting the UserControl's property in RadGrid's ItemCreated property.  Everything works fine until a postback is caused by the UserControl - RadGrid's ItemCreated event fires, but the DataItem property (e.Item.DataItem) of GridItemEventArgs is null after postback. So I can't set the UserControl's view model property. 

Is there a way to retain the DataItem even after a postback caused within the EditForm/UserControl?

Thanks in advance!
Makoto

5 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 07 Jul 2012, 02:56 PM
Hello,

You can get value in "e.Item.DataItem" only in inside ItemdataBound event.

If you want to access e.Item.DataItem outside the itemdatabound event then it is always is null.

So try to achieve your functionality by some other way. like using Datakey, Column..etc

Thanks,
Jayesh Goyani
0
Makoto
Top achievements
Rank 1
answered on 09 Jul 2012, 07:47 PM
Thanks Jayesh for a prompt response - the ItemDataBound event for the grid doesn't fire on postback raised from the UserControl/EditForm...so using ItemDataBound event unfortunately won't work in this case...

I ended up setting the business object to the UserControl from the Session variable in ItemCreated event - not an elegant approach...any thoughts on that?
0
Tsvetoslav
Telerik team
answered on 10 Jul 2012, 05:53 AM
Hello Makoto,

The best way to go about your scenario is to make the controls/properites in your user control retrieve their values from ViewState on post back. (Some of them might do this automatically, e.g. if you have a TextBox in the user control it will retrieve its value automatically from ViewState). In this way, the logic in the ItemCreated event won't be needed on post back.

Hope it helps.

Greetings,
Tsvetoslav
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Makoto
Top achievements
Rank 1
answered on 10 Jul 2012, 09:25 PM
Hi Tsvetoslav,

Thanks for the recommendation. The data UserControl binds to (supplied by RadGrid, which is bound to a List object of custom business objects) is a custom business object. The controls on the UserControl are directly bound to the business object.

I thought it's usually recommended not to store such object in ViewState due to the potential for complexity of making the business object to be Serializable (because of types of objects used within) and possible performance degredation from using ViewState. Would it still be safe to do that in this case?

This may be more of the discussion about UserControl design/implementation. But any feedback will be appreciated.
0
Tsvetoslav
Telerik team
answered on 11 Jul 2012, 09:14 AM
Hi Makoto,

Indeed, you are correct about serialization but as long as  your business objects are marked as Serializable, there should be no problem.


Regards,
Tsvetoslav
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Makoto
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Makoto
Top achievements
Rank 1
Tsvetoslav
Telerik team
Share this question
or