I have a page that loads a list of controls based on report criteria. I've been converting some of these controls to Telerik, but I also have some controls that are custom controls. I've narrowed down the issue to when an ASP.NET control (ASP from the dropdown) gets loaded after a Telerik control (Telerik from the dropdown), I get a "Failed to load viewstate" error. It also happens if both controls are Telerik controls, but does not happen if all controls are ASP.NET controls.
DynamicControls.aspx
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="DynamicControls.aspx.vb" Inherits="TestCode.DynamicContrls" %>
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>
<!DOCTYPE html>
<
html
xmlns
=
"http://www.w3.org/1999/xhtml"
>
<
head
runat
=
"server"
>
<
title
></
title
>
</
head
>
<
body
>
<
form
id
=
"form1"
runat
=
"server"
>
<
div
>
<
telerik:RadScriptManager
ID
=
"RadScriptManager1"
Runat
=
"server"
>
</
telerik:RadScriptManager
>
<
asp:DropDownList
ID
=
"cboCriteria"
runat
=
"server"
AutoPostBack
=
"True"
>
<
asp:ListItem
>Select a form</
asp:ListItem
>
<
asp:ListItem
Value
=
"Telerik"
>Form With Telerik Control</
asp:ListItem
>
<
asp:ListItem
Value
=
"ASP"
>Form With ASP Control</
asp:ListItem
>
</
asp:DropDownList
>
<
br
/>
<
asp:PlaceHolder
ID
=
"PlaceHolder1"
runat
=
"server"
></
asp:PlaceHolder
>
<
br
/>
</
div
>
</
form
>
</
body
>
</
html
>
DynamicControls.vb
Imports Telerik.Web.UI
Public Class DynamicContrls
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'If Not Session("ControlsCreated") = Nothing Then
CreateControl(cboCriteria.SelectedItem.Value)
'End If
End Sub
Private Sub CreateControl(ControlType As String)
Select Case ControlType
Case "Telerik"
RenderTelerikControls()
Case "ASP"
RenderASPControls()
End Select
Session("ControlsCreated") = True
End Sub
Private Sub RenderTelerikControls()
Dim txtFirst As New RadTextBox
txtFirst.ID = "txtTelerik" & Guid.NewGuid.ToString
PlaceHolder1.Controls.Add(txtFirst)
PlaceHolder1.Controls.Add(New LiteralControl("<
BR
>"))
End Sub
Private Sub RenderASPControls()
Dim txtFirst As New TextBox
txtFirst.ID = "txtASP" & Guid.NewGuid.ToString
PlaceHolder1.Controls.Add(txtFirst)
PlaceHolder1.Controls.Add(New LiteralControl("<
BR
>"))
End Sub
Protected Sub cboCriteria_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboCriteria.SelectedIndexChanged
' For this example, simply reload the page.
End Sub
End Class