Click here to Skip to main content
15,883,961 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi,
I have created dynamic controls in asp.net 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.

MyDropDown.ascx

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


Code Behind:-
C#
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; } }

        /*  TRIED WITH FOLLOWING OPTION BUT CAN'T LUCK :)
         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)
        {
            base.OnInit(e);
            this.ddlList.SelectedIndexChanged += new EventHandler(ddlList_SelectedIndexChanged);
        }
        public void ClearSelection()
        {
            ddlList.ClearSelection();
        }
        private void ddlList_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedIndexChanged(e);
        }
        protected void SelectedIndexChanged(EventArgs e)
        {
            if (DropDownSelectedIndexChanged != null)
            {
                DropDownSelectedIndexChanged(this, e);
            }
        }


2)I have called that .ascx control in text.aspx page like
C#
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);
            newCell.Controls.Add(ddl);
            row.Controls.Add(newCell);
            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
v3

Hi,

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[^]
 
Share this answer
 
Comments
Member 12855870 3-Jul-17 9:11am    
hai
How to solve the problem
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.
 
Share this answer
 
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900