Dynamic RadComboBoxes

6 posts, 1 answers
  1. Aleks
    Aleks avatar
    2 posts
    Member since:
    Jul 2012

    Posted 11 Jul 2012 Link to this post

    Hi, Guys!

    I try dynamically create RadComboBox controls inside ASP.NET PlaceHolder control.

    But i got next exception:
    "RegisterRequiresControlState can only be called before and during PreRender".

    Can anyone help me?
  2. Nencho
    Admin
    Nencho avatar
    1820 posts

    Posted 11 Jul 2012 Link to this post

    Hello Aleks,

    You could check the following blog post, where the issue you experience, is well described.

    Greetings,
    Nencho
    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.
  3. Aleks
    Aleks avatar
    2 posts
    Member since:
    Jul 2012

    Posted 11 Jul 2012 Link to this post

    Thanks for your reply, but this post doesn't touch my problem even if it describes the same error message.

    I had created test page to demonstrate the issue.

    Default.aspx code:
    <%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
        Inherits="DynamicRadComboBoxes._Default" %>
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <head id="Head1" runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server" AsyncPostBackTimeout="1024">
        </telerik:RadScriptManager>
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="pnlOnlineBookingContent">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="pnlOnlineBookingContent" LoadingPanelID="ralpOnlineBooking" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>
        <telerik:RadAjaxLoadingPanel runat="server" ID="ralpOnlineBooking" Skin="Default">
        </telerik:RadAjaxLoadingPanel>
        <div>
            <asp:Panel runat="server" ID="pnlOnlineBookingContent">
                <asp:PlaceHolder runat="server" ID="phSearchItems" />
                <div style="clear: both;">
                </div>
                <br />
                <asp:Panel runat="server" ID="pnlAddMoreService">
                    <div>
                        <asp:LinkButton runat="server" ID="btnAddItem" Visible="true" OnClick="btnAddItem_Click">
                            Add More Service
                        </asp:LinkButton>
                    </div>
                </asp:Panel>
            </asp:Panel>
        </div>
        </form>
    </body>
    </html>

    Default.aspx.cs code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Telerik.Web.UI;
    using System.IO;
     
    namespace DynamicRadComboBoxes
    {
        public partial class _Default : System.Web.UI.Page
        {
            public List<RadComboBox> SearchItems
            {
                get { return (List<RadComboBox>)Session["SearchItems"]; }
                set { Session["SearchItems"] = value; }
            }
     
            protected void Page_Init(object sender, EventArgs e)
            {
                if (Page.IsPostBack)
                    RestoreControls();
            }
     
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                    ConfigureControls();
            }
     
            private void ConfigureControls()
            {
                SearchItems = new List<RadComboBox>();
                CreateComboBox();
            }
     
            private void RestoreControls()
            {
                foreach (var combo in SearchItems)
                    phSearchItems.Controls.Add(combo);
            }
     
            private void CreateComboBox()
            {
                var combo = new RadComboBox();
                combo.ID = string.Format("cboTest_{0}", Path.GetFileNameWithoutExtension(Path.GetTempFileName()));
     
                SearchItems.Add(combo);
                phSearchItems.Controls.Add(combo);
            }
     
            protected void btnAddItem_Click(object sender, EventArgs e)
            {
                CreateComboBox();
            }
        }
    }

    The logic is next:
    - When page loads in first time - we create one RadComboBox inside PlaceHolder and add it to the list stored in Session.
    - When user click button - in async request we add one more RadComboBox inside PlaceHolder and add it to the list stored in Session. 
    - I got this error ("RegisterRequiresControlState can only be called before and during PreRender") inside RestoreControls method.

    Where i'm doing wrong?
  4. Answer
    Nencho
    Admin
    Nencho avatar
    1820 posts

    Posted 12 Jul 2012 Link to this post

    Hello Aleks,

    As we consider transferring data, storing objects in session is not a good approach. You could simply store the ID's of the newly added controls, and recreate them when necessary. Please find the attached sample, where you could observe the implemented approach.


    Kind regards,
    Nencho
    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.
  5. Roukaya
    Roukaya avatar
    17 posts
    Member since:
    Aug 2012

    Posted 04 Aug 2012 Link to this post

    thanks  Nencho for your helpful example , exactly what i was looking for ,
    but this sample allow us to restore comboboxes without their datasource , (they are restored empty)..
    any way to restore filled combobox ??

    thanks in advance ..
  6. Nencho
    Admin
    Nencho avatar
    1820 posts

    Posted 07 Aug 2012 Link to this post

    Hi Aleks,

    Try to add the new  RadComboBox control to the PlaceHolder immediately after its creation. After that you could bind the data to RadComboBox and the selection will persist through the postback. Please alter the CreateComboBox() method in a following manner:

    private void CreateComboBox()
       {
           var combo = new RadComboBox();
           phSearchItems.Controls.Add(combo);
     
           combo.ID = string.Format("cboTest_{0}", Path.GetFileNameWithoutExtension(Path.GetTempFileName()));
     
           combo.Items.Add(new RadComboBoxItem("item1", "1"));
           combo.Items.Add(new RadComboBoxItem("item2", "2"));
           combo.Items.Add(new RadComboBoxItem("item3", "3"));
     
           SearchItemsIDs.Add(combo.ID);
       }

    In addition, please find the slightly modified sample attached.

    Kind regards,
    Nencho
    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.
Back to Top