Click here to Skip to main content
13,195,564 members (68,123 online)
Rate this:
Please Sign up or sign in to vote.
I have created dynamic controls in web application.for that i followed below steps
1) I have created my DropDown List custom(.ascx) control in which i have mentioned following properties to enahance it's existing functionality.


<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyTestDropDown.ascx.cs" Inherits="MyTest.MyTestDropDown" %>
<asp:DropDownList ID="ddlList" runat="server">

Code Behind:-
public Object DataSource { get { return ddlList.DataSource; } set { ddlList.DataSource = value; } }
        public string DataTextField { get { return ddlList.DataTextField; } set { ddlList.DataTextField = value; } }
        public string DataValueField { get { return ddlList.DataValueField; } set { ddlList.DataValueField = value; } }
        public bool AutoPostBack { get { return ddlList.AutoPostBack; } set { ddlList.AutoPostBack = value; } }
        public string SelectedValue { get { return ddlList.SelectedValue; } set { ddlList.SelectedValue = value; } }
        public string SelectedText { get { return ddlList.SelectedItem.Text; } set { ddlList.SelectedItem.Text = value; } }
        public int SelectedIndex { get { return ddlList.SelectedIndex; } set { ddlList.SelectedIndex = value; } }
        public Unit Width { get { return ddlList.Width; } set { ddlList.Width = value; } }
        public ListItemCollection Items { get { return ddlList.Items; } }
         public string QueryResultSeparater { get { return ViewState["QueryResultSeparater"].ToString(); } set { ViewState["QueryResultSeparater"] = value; } }
        public string Query { get { return ViewState["Query"].ToString(); } set { ViewState["Query"] = value; } }
        public string Name { get { return ViewState["Name"].ToString(); } set { ViewState["Name"] = value; } }
        public string DisplayText { get { return ViewState["DisplayText"].ToString(); } set { ViewState["DisplayText"] = value; } } */
        public string QueryResultSeparater { get; set; }
        public string Query { get; set; }
        public string Name { get; set; }
        public string DisplayText { get; set; }
        public event EventHandler DropDownSelectedIndexChanged;
        protected void Page_Load(object sender, EventArgs e)
        protected override void OnInit(EventArgs e)
            this.ddlList.SelectedIndexChanged += new EventHandler(ddlList_SelectedIndexChanged);
        public void ClearSelection()
        private void ddlList_SelectedIndexChanged(object sender, EventArgs e)
        protected void SelectedIndexChanged(EventArgs e)
            if (DropDownSelectedIndexChanged != null)
                DropDownSelectedIndexChanged(this, e);

2)I have called that .ascx control in text.aspx page like
protected void Page_Load(object sender, EventArgs e)
 TableCell newCell = new TableCell();                      
            MyDropDown customDropdown = LoadControl(.ASCX PATH ...) as MyDropDown;
            MyDropDown ddl = customDropdown;
            ddl.ID = //name ofcontrol .....;
            ddl.DisplayText = //headertext.....
            ddl.AutoPostBack = true;
            ddl.Query = ;//Data query
            ddl.Width = Unit.Pixel(100);
            ddl.Name = //name of control;          
            ddl.DropDownSelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
            DataTable.Rows.Add(row); // Table object and row object is already in my page so not mentioning that logic here..

3) When I publish my application , some time I am getting following error in errorlog file where i save error.

ERROR:=> LoadViewStateRecursive Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.

Please help me if any one know better solution for the above problem.
Thanks in Advance.
Posted 5-Feb-13 19:20pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


You have to (re)create your dynamic controls in the Page_Init event - which occurs before the ViewState is loaded.

You can create controls for the first time in Page_Load or most events - just make sure to recreate them in Page_Init when the page gets loaded again.

If you need to keep track of what controls to recreate you have to use non-viewstate storage during Page_Init (ie Session variables) since the viewstate has *not* been loaded.

A nice article explaining the ASP.NET page lifecycle do what:
ASP.NET Application and Page Life Cycle[^]
Member 12855870 3-Jul-17 9:11am
How to solve the problem
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Thanks for posting answer.
yes I have done like that but still sometime issue was coming so instead of .ascx control i have created my custom control so now waiting lets see :) issue still persist or not.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

I had the same issue. This issue was at client end but it didn't occur in my local system.
After hours of googling, i had written EnableViewState="false" to my table tag in aspx page which has all the dynamic controls and then i removed all the viewstate variables and instead i created some hidden textboxes in the aspx page and accepted DB values into them in code behind and used them throughout my code. It then solved my problem.
But still, i couldn't figure out what was exactly the problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171019.1 | Last Updated 1 Sep 2016
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100