Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to 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
 
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyTestDropDown.ascx.cs" Inherits="MyTest.MyTestDropDown" %>
<asp:DropDownList ID="ddlList" runat="server">
</asp:DropDownList>
 
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; } }
 
        /*  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
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 5-Feb-13 19:20pm
Edited 5-Feb-13 19:26pm
Tadit Dash83.3K
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
Rate this: bad
good
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 Smile | :) issue still persist or not.
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 457
1 OriginalGriff 380
2 Shai Vashdi 335
3 Peter Leow 171
4 Emre Ataseven 165
0 Sergey Alexandrovich Kryukov 8,944
1 OriginalGriff 5,280
2 Peter Leow 3,985
3 Maciej Los 3,535
4 Abhinav S 3,218


Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid