Is this scenario possible? (How do I keep track of dynamically loaded controls?)

2 posts, 0 answers
  1. Boris
    Boris avatar
    275 posts
    Member since:
    Oct 2012

    Posted 07 Apr 2014 Link to this post

    I'm not sure whether I have a Telerik problem or a more generic Asp.Net problem.  (I'm getting that 'Failed to load viewstate' error.)

    I have a RadComboBox set up in the RadAjaxManager.  The box in turn has a RadTabStrip and an Asp:Panel as it's updated controls.

    What I would like to do is when certain values of the box are selected, is to load one of several ASCXs I have defined.  If I change the value of the dropdown to a value that does not have an ASCX associated with it, I want to dispose of whatever ASCX has already been loaded. Or if a different ASCX is selected, I want to unload the old ASCX and load the new.  The ASCX is to be loaded into the Asp:Panel.   In order to capture the values entered into the form, I reload the form on the Page_Load postback event (using the viewstate to get the values).  Up till now I haven't had to worry about this because I simple loaded all the forms at once and simply used AJAX to show/hide them as necessary.

    This scheme works fine if I select a single ASCX related value, load the control, and then immediately hit save or cancel.

    However if I change the dropdown multiple times, things seem to break down.  I seem to lose the Ajax if that's the right way to put it.  (In any case it seems to stop loading the form I've selected and if I hit cancel, I get that 'Failed to load viewstate' error.

    How do I keep the viewstate under control?  Suggestions?
  2. Krsnaraja
    Krsnaraja avatar
    9 posts
    Member since:
    Aug 2007

    Posted 08 Apr 2014 in reply to Boris Link to this post

    Hi,

    The problem is not specific to Telerik controls. To address your issue you need to reload your dynamic controls on each and every postback on the Page_Load event at the latest. 

    <form id="form1" runat="server">
            <div>
                <asp:PlaceHolder ID="PlaceHolder1" runat="server" />
                <asp:DropDownList runat="server" ID="DropDownList1" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                    <asp:ListItem Text="--Select--" />
                    <asp:ListItem Text="TextBox" Value="TextBox" />
                    <asp:ListItem Text="Button" Value="Button" />
                </asp:DropDownList>
            </div>
        </form>

    /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            /*
             * check for a session that you set when you create dynamic control.
             * this way it will no be created on very first time page loads
             * and it will re-create appropriate controls after every postback
             * which is required when you code for dynamic controls
             */
            if (Session["ControlsCreated"] != null)
                CreateControl(DropDownList1.SelectedItem.Value);
        }
     
        /// <summary>
        /// Creates the control.
        /// </summary>
        /// <param name="controlToCreate">The control to create.</param>
        private void CreateControl(string controlToCreate)
        {
            PlaceHolder1.Controls.Clear();
     
            switch (controlToCreate)
            {
                case "TextBox":
                    var textBox1 = new TextBox {ID = "TextBox1"};
                    PlaceHolder1.Controls.Add(textBox1);
                    break;
                case "Button":
                    var button1 = new Button {Text = "Click!", ID = "Button1"};
                    PlaceHolder1.Controls.Add(button1);
                    break;
                default:
                    // do nothing
                    break;
            }
     
            // flag session variable so the control is recreated on every Page_Load event
            Session["ControlsCreated"] = true;
        }
        /// <summary>
        /// Handles the SelectedIndexChanged event of the DropDownList1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Create dynamic controls
            CreateControl(DropDownList1.SelectedItem.Value);
        }

    Let us know for more help.

    Thanks,
    Krishna
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top