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

ViewState error when dynamically loading Telerik controls

2 Answers 217 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
JenMaryland
Top achievements
Rank 1
JenMaryland asked on 27 Mar 2020, 07:52 PM

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>
 
<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

2 Answers, 1 is accepted

Sort by
0
JenMaryland
Top achievements
Rank 1
answered on 30 Mar 2020, 01:43 PM

To add, while I could disable the viewstate for all dynamic controls, which does work, these controls disappear when there is a postback. The purpose of the dynamic controls is to generate criteria filters for reports/queries. The problem stems from when there's a postback and new controls are being created.

1 - A dropdownlist postback is made - ASP.NET control is created.
2 - Another dropdownlist postback is made - Tries to create a telerik control and errors out. 

0
JenMaryland
Top achievements
Rank 1
answered on 30 Mar 2020, 07:05 PM

Ok, I think I found it after many hours of digging around. I simple disabled the viewstate of the panel, but not the individual dynamic controls. I also found Request.Form([ControlName]) in order to get the value of the dropdown on init or load.

---

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim formValue As String = ""
    If Not String.IsNullOrEmpty(Request.Form("cboCriteria")) Then
      formValue = Request.Form("cboCriteria")
      Response.Write("<HR>" & formValue & "<HR>")
    End If

    CreateControl(formValue)

  End Sub

Tags
General Discussions
Asked by
JenMaryland
Top achievements
Rank 1
Answers by
JenMaryland
Top achievements
Rank 1
Share this question
or