locked
view state for composite control does not restored RRS feed

  • Question

  • User-1945566640 posted

    Hi, All

    I've developed a server control name mytreeview.

    my problem is after postback, the control does not load viewstate, and so it disappears from page:

    here is my classes:

    Webcontrol:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    
    namespace Ramin.WebControls
    {
        [Serializable]
        [DefaultProperty("Nodes")]
        [ToolboxData("<{0}:MyTreeView runat=server></{0}:MyTreeView>")]
        public class MyTreeView : CompositeControl
        {
            public List<MyTreeNode> Nodes
            {
                get { return (List<MyTreeNode>)(ViewState["Nodes"] ?? new List<MyTreeNode>()); }
                set { ViewState["Nodes"] = value; }
            }
    
            public MyTreeView()
            {
                Nodes = new List<MyTreeNode>();
            }
    
            protected override void CreateChildControls()
            {
                if (Nodes.Count > 0)
                {
                    HtmlGenericControl div = new HtmlGenericControl("div");
                    HtmlGenericControl ul = new HtmlGenericControl("ul");
    
                    div.Attributes.Add("class", "treeList roundcorner");
                    ul.Attributes.Add("class", "arbo");
    
                    div.Controls.Add(ul);
    
                    foreach (MyTreeNode node in Nodes)
                        ul.Controls.Add(node);
                    Controls.Add(ul);
                }
                base.CreateChildControls();
            }
    
            protected override void Render(HtmlTextWriter writer)
            {
                base.RenderChildren(writer);
            }
        }
    }

    MyTreeNode:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    namespace Ramin.WebControls
    {
        [Serializable]
        [DefaultProperty("Text")]
        [ToolboxData("<{0}:MyTreeNode runat=server></{0}:MyTreeNode>")]
        public class MyTreeNode : WebControl
        {
            public bool Closed
            {
                get { return (bool)(ViewState["Closed"] ?? true); }
                set { ViewState["Closed"] = value; }
            }
    
            public string Text
            {
                get { return (string)(ViewState["Text"] ?? ""); }
                set { ViewState["Text"] = value; }
            }
    
            public List<MyTreeNode> Children
            {
                get { return (List<MyTreeNode>)(ViewState["Children"] ?? new List<MyTreeNode>()); }
                set { ViewState["Children"] =value; }
            }
    
            public MyTreeNode()
            {
                Children = new List<MyTreeNode>();
            }
    
            protected override void CreateChildControls()
            {
                HtmlGenericControl li = new HtmlGenericControl("li");
                HtmlGenericControl span = new HtmlGenericControl("span");
                HtmlGenericControl anchorSpan = new HtmlGenericControl("span");
                HtmlAnchor a = new HtmlAnchor();
    
                if (Closed)
                    li.Attributes.Add("class", "close");
                if (Children.Count > 0)
                    a.Attributes.Add("class", "folder");
    
                if (Children.Count > 0)
                    span.Attributes.Add("class", "toggle");
    
                anchorSpan.InnerText = Text;
    
                li.Controls.Add(span);
                li.Controls.Add(a);
                a.Controls.Add(anchorSpan);
                Controls.Add(li);
    
                if (Children.Count > 0)
                {
                    HtmlGenericControl ul = new HtmlGenericControl("ul");
                    Controls.Add(ul);
    
                    foreach (MyTreeNode node in Children)
                        ul.Controls.Add(node);
                    li.Controls.Add(ul);
                }
                base.CreateChildControls();
            }
        }
    
    
    }

    Web From code behind:

    using System;
    using System.Web.UI.WebControls;
    using Ramin.WebControls;
    
    namespace Ifa.UI
    {
        public partial class WebForm2 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    for (int i = 0; i < 10; i++)
                    {
                        MyTreeNode node1 = new MyTreeNode();
                        node1.Text = "text" + i.ToString();
    
                        for (int j = 0; j < 5; j++)
                        {
                            MyTreeNode node2 = new MyTreeNode();
                            node2.Text = "text" + i.ToString() + " " + j.ToString();
                            node1.Children.Add(node2);
                        }
                        mytreeview1.Nodes.Add(node1);
                    }
    
    
                    for (int i = 0; i < 10; i++)
                    {
                        TreeNode node1 = new TreeNode();
                        node1.Text = "text" + i.ToString();
    
                        for (int j = 0; j < 5; j++)
                        {
                            TreeNode node2 = new TreeNode();
                            node2.Text = "text" + i.ToString() + " " + j.ToString();
                            node1.ChildNodes.Add(node2);
                        }
                        treeview1.Nodes.Add(node1);
                    }
                }
            }
        }
    }

    mytreeview1(my web control) disappears after postback. however asp.net treeview (treeview1) remain unchanged.

    why mytreeview1 does not load viewstate?

    tanks in advance.

    Saturday, September 17, 2011 12:05 PM

Answers

  • User-1945566640 posted

    tanks mr.decker for your assistant.

    the error is because of TreeNode Class. it inherits from webcontrol class which is not serializeable, so I've changed it to typical class.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Web.UI;
    
    namespace Ramin.WebControls
    {
        [Serializable]
        [DefaultProperty("Text")]
        public class MyTreeNode
        {
            private bool closed;
            public bool Closed
            {
                get { return closed; }
                set
                {
                    closed = value;
                    foreach (MyTreeNode node in Children)
                        node.Closed = true;
                }
            }
    
            private bool selected;
            public bool Selected
            {
                get { return selected; }
                set
                {
                    selected = value;
                    foreach (MyTreeNode node in Children)
                        node.Selected = false;
                }
            }
    
            public string Text { get; set; }
            public List<MyTreeNode> Children { get; set; }
            public MyTreeNode Parent { get; set; }
    
            public MyTreeNode()
            {
                Children = new List<MyTreeNode>();
                Closed = true;
            }
    
            public void Render(HtmlTextWriter writer)
            {
                writer.WriteBeginTag("li");
                if (Closed)
                    writer.WriteAttribute("class", "close");
    
                writer.Write(HtmlTextWriter.TagRightChar);
    
                writer.WriteBeginTag("span");
                if (Children.Count > 0)
                    writer.WriteAttribute("class", "toggle");
    
                writer.Write(HtmlTextWriter.TagRightChar);
                writer.WriteEndTag("span");
    
                writer.WriteBeginTag("a");
    
                if (Selected && Children.Count > 0)
                    writer.WriteAttribute("class", "current folder");
                else
                {
                    if (Children.Count > 0)
                        writer.WriteAttribute("class", "folder");
                    else if (Selected)
                        writer.WriteAttribute("class", "current");
                }
    
                writer.Write(HtmlTextWriter.TagRightChar);
    
                writer.WriteBeginTag("span");
                writer.Write(HtmlTextWriter.TagRightChar);
                writer.Write(Text);
                writer.WriteEndTag("span");
    
                writer.WriteEndTag("a");
    
                if (Children.Count > 0)
                {
                    writer.WriteLine("<ul>");
                    foreach (MyTreeNode node in Children)
                        node.Render(writer);
                    writer.WriteLine("</ul>");
                }
    
            }
        }
    
    
    }

    tanks.



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 21, 2011 6:41 AM

All replies

  • User3866881 posted

    Hello:)

    Everytime I see:

    public MyTreeView()
           
    {
               
    Nodes = new List<MyTreeNode>();
           
    } 
    This will called every postback, and your nodes is always the blank one.

    Solution: Please remove the constructor and have a try.


    Sunday, September 18, 2011 10:01 PM
  • User-1945566640 posted

    hi. mr decker.

    i've applied you solution. now i'm getting:

    Type 'System.Web.UI.WebControls.WebControl' in Assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.

    tank u.

    Monday, September 19, 2011 2:39 PM
  • User3866881 posted

    hi. mr decker.

    i've applied you solution. now i'm getting:

    Type 'System.Web.UI.WebControls.WebControl' in Assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.

    tank u.

    Hello, Please debug your app and tell me at which statement the error is thrown out?

    Thx again

    Monday, September 19, 2011 8:52 PM
  • User-1945566640 posted

    tanks mr.decker for your assistant.

    the error is because of TreeNode Class. it inherits from webcontrol class which is not serializeable, so I've changed it to typical class.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Web.UI;
    
    namespace Ramin.WebControls
    {
        [Serializable]
        [DefaultProperty("Text")]
        public class MyTreeNode
        {
            private bool closed;
            public bool Closed
            {
                get { return closed; }
                set
                {
                    closed = value;
                    foreach (MyTreeNode node in Children)
                        node.Closed = true;
                }
            }
    
            private bool selected;
            public bool Selected
            {
                get { return selected; }
                set
                {
                    selected = value;
                    foreach (MyTreeNode node in Children)
                        node.Selected = false;
                }
            }
    
            public string Text { get; set; }
            public List<MyTreeNode> Children { get; set; }
            public MyTreeNode Parent { get; set; }
    
            public MyTreeNode()
            {
                Children = new List<MyTreeNode>();
                Closed = true;
            }
    
            public void Render(HtmlTextWriter writer)
            {
                writer.WriteBeginTag("li");
                if (Closed)
                    writer.WriteAttribute("class", "close");
    
                writer.Write(HtmlTextWriter.TagRightChar);
    
                writer.WriteBeginTag("span");
                if (Children.Count > 0)
                    writer.WriteAttribute("class", "toggle");
    
                writer.Write(HtmlTextWriter.TagRightChar);
                writer.WriteEndTag("span");
    
                writer.WriteBeginTag("a");
    
                if (Selected && Children.Count > 0)
                    writer.WriteAttribute("class", "current folder");
                else
                {
                    if (Children.Count > 0)
                        writer.WriteAttribute("class", "folder");
                    else if (Selected)
                        writer.WriteAttribute("class", "current");
                }
    
                writer.Write(HtmlTextWriter.TagRightChar);
    
                writer.WriteBeginTag("span");
                writer.Write(HtmlTextWriter.TagRightChar);
                writer.Write(Text);
                writer.WriteEndTag("span");
    
                writer.WriteEndTag("a");
    
                if (Children.Count > 0)
                {
                    writer.WriteLine("<ul>");
                    foreach (MyTreeNode node in Children)
                        node.Render(writer);
                    writer.WriteLine("</ul>");
                }
    
            }
        }
    
    
    }

    tanks.



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 21, 2011 6:41 AM