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

RadButton and ViewState

2 Answers 85 Views
Button
This is a migrated thread and some comments may be shown as answers.
Patrick
Top achievements
Rank 1
Patrick asked on 17 Oct 2012, 05:36 PM
The situation is as follows:
I'm trying to dynamically create RadButtons (type = option button) in an ASP:GridView.  (maybe this should be in a RadGrid, but that's another conversation for another time)

What I did was dynamically add the RadButton on a button.click method as follows:

Dim dupOptionTop As RadButton = New RadButton()
dupOptionTop.GroupName = dgr.Cells(lnColumn).Text
dupOptionTop.ID = dupFile.FILEID
dupOptionTop.AutoPostBack = False
dupOptionTop.ButtonType = RadButtonType.ToggleButton
dupOptionTop.ToggleType = ButtonToggleType.Radio
dupOptionTop.ForeColor = Color.Brown
 
Dim dupOption As RadButtonToggleState = New RadButtonToggleState()
dupOptionTop.Text = dupFile.FILEID
dupOptionTop.ToggleStates.Add(dupOption)
dupOptionTop.ToggleStates.Add(dupOption)
dgr.Cells(lnMsgColumn + 1).Controls.Add(dupOptionTop)

I figure that the user could select from 2 or more choices, then iterate through the controls collection of a GridView cell to find which choice they selected and move on.

Unfortunately, while the new controls are on the users screen, they are unavailable for use in the code-behind.  I'm now well aware that the code-behind can't see dynamically created controls unless they are set up in the Page_Init file.  The problem is that it needs to have the data from the GridView loaded in order to create the controls, and that data won't load until AFTER the Page_Init event.

So I was thinking perhaps I can use the ViewState as follows in the Page_Load, and then use those to repopulate the GridView with the RadButtons:

Dim _options = New List(Of RadButton)
ViewState.Add("options", _options)

And then I run into this problem:
Type 'Telerik.Web.UI.RadButton' in Assembly 'Telerik.Web.UI, Version=2012.1.411.35, Culture=neutral, PublicKeyToken=121fae78165ba3d4' is not marked as serializable.
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.Runtime.Serialization.SerializationException: Type 'Telerik.Web.UI.RadButton' in Assembly 'Telerik.Web.UI, Version=2012.1.411.35, Culture=neutral, PublicKeyToken=121fae78165ba3d4' is not marked as serializable.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Is there any way to get around this issue, or is there perhaps a much better way to accomplish this goal?

Thanks for any help you may have.

2 Answers, 1 is accepted

Sort by
0
Accepted
Slav
Telerik team
answered on 22 Oct 2012, 11:03 AM
Hi Patrick,

If I understand your scenario correctly, you are dynamically inserting RadButtons in a GridView and you want to persist the information about these buttons through postback. To add the buttons you can either create them on Page_Init and load them in the grid view later when the control is data-bound or configure the template of the grid view programmatically.

As mentioned in the exception message, the RadButton is not marked as serializable thus it cannot be stored in the ViewState. Nevertheless, you can create your own class that will be serializable and will contain the settings of the RadButtons that you create dynamically. Such a class will allow you to store the settings in ViewState and use them when creating the buttons.

Greetings,
Slav
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
Patrick
Top achievements
Rank 1
answered on 23 Oct 2012, 07:53 PM
Hi Slav,
Ah... I see.  It turns out that I chose not to use RadButtons after all.  I decided on using a RadComboBox, which I can set up in the ASPX and I can dynamically create items with no problem.

Unfortunately, changes in the specs now makes a basic RadComboBox no good; now I'll need a RadGrid inside of a RadComboBox, and I have to deal with issues regarding that.  But since that's outside of the scope of this discussion, I will mark your reply as the answer and address the new issue on a separate message and forum.

Thanks for your help,
Patrick
Tags
Button
Asked by
Patrick
Top achievements
Rank 1
Answers by
Slav
Telerik team
Patrick
Top achievements
Rank 1
Share this question
or