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

Dynamic RadComboBoxes

5 Answers 93 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Aleks
Top achievements
Rank 1
Aleks asked on 11 Jul 2012, 10:40 AM
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?

5 Answers, 1 is accepted

Sort by
0
Nencho
Telerik team
answered on 11 Jul 2012, 01:58 PM
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.
0
Aleks
Top achievements
Rank 1
answered on 11 Jul 2012, 02:20 PM
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?
0
Accepted
Nencho
Telerik team
answered on 12 Jul 2012, 11:47 AM
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.
0
Roukaya
Top achievements
Rank 2
answered on 04 Aug 2012, 07:44 AM
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 ..
0
Nencho
Telerik team
answered on 07 Aug 2012, 12:38 PM
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.
Tags
ComboBox
Asked by
Aleks
Top achievements
Rank 1
Answers by
Nencho
Telerik team
Aleks
Top achievements
Rank 1
Roukaya
Top achievements
Rank 2
Share this question
or