{"id":236,"date":"2010-03-29T09:58:28","date_gmt":"2010-03-29T14:58:28","guid":{"rendered":"http:\/\/hoolihan.net\/blog-tim\/?p=236"},"modified":"2010-09-03T10:33:37","modified_gmt":"2010-09-03T15:33:37","slug":"handling-persistance-in-asp-net-with-dynamic-controls","status":"publish","type":"post","link":"http:\/\/hoolihan.net\/blog-tim\/2010\/03\/29\/handling-persistance-in-asp-net-with-dynamic-controls\/","title":{"rendered":"Handling Persistance in Asp.Net with Dynamic Controls"},"content":{"rendered":"<p>I worked on a project a couple of years ago using dynamic controls in an Asp.Net WebForms environment.  Managing dynamic controls can be a real hassle.  <\/p>\n<p>So when I read Dave Reed&#8217;s <a href=\"http:\/\/weblogs.asp.net\/infinitiesloop\/archive\/2006\/08\/03\/Truly-Understanding-Viewstate.aspx\">great article about Viewstate<\/a>, I wondered if I could do it better now.  I worked up a simple example using no Viewstate where values persist across postbacks.  Going to the page new resets everything.  <\/p>\n<p>The one piece I don&#8217;t like is that it uses session for a control count.  But it&#8217;s a single Int32 per user that get&#8217;s stored in session, so it could be worse.  Anyway, here&#8217;s the code&#8230;<\/p>\n<p><em>Note: the aspx page has a placeholder (phOne) and two buttons (btnAdd) and (btnRemove).  EnableViewstate is false on phOne.<\/em><\/p>\n<pre>\r\n<code>using System;\r\nusing System.Web.UI;\r\nusing System.Web.UI.WebControls;\r\n\r\nnamespace Web\r\n{\r\n    public partial class Dynamic : System.Web.UI.Page\r\n    {\r\n        private const string KeyNumControls = \"DynamicPage_NumControls\";\r\n        private const int DefaultControlCount = 2;\r\n\r\n        protected override void OnInit(EventArgs e)\r\n        {\r\n            CreateControls(NumberOfControls);\r\n            base.OnInit(e);\r\n        }\r\n\r\n        protected override void OnLoad(EventArgs e)\r\n        {\r\n            if (!Page.IsPostBack)\r\n            {\r\n                CreateControls(DefaultControlCount);\r\n            }\r\n        }\r\n\r\n        private void CreateControls(int controlCount)\r\n        {\r\n            NumberOfControls = controlCount;\r\n            while(phOne.Controls.Count &gt; NumberOfControls)\r\n            {\r\n                RemoveLastControl();\r\n            }\r\n            while(phOne.Controls.Count &lt; NumberOfControls)\r\n            {\r\n                CreateControl();\r\n            }\r\n        }\r\n\r\n        private void RemoveLastControl()\r\n        {\r\n            phOne.Controls.RemoveAt(phOne.Controls.Count - 1);\r\n        }\r\n\r\n        private void CreateControl()\r\n        {\r\n            phOne.Controls.Add(new TextBox { EnableViewState = false });\r\n        }\r\n\r\n        protected int NumberOfControls\r\n        {\r\n            get\r\n            {\r\n                Session[KeyNumControls] = Session[KeyNumControls] ?? \r\n                                        DefaultControlCount;\r\n                return (int)Session[KeyNumControls];\r\n            }\r\n            set { Session[KeyNumControls] = value; }\r\n        }\r\n\r\n        protected void BtnAddClick(object sender, EventArgs e)\r\n        {\r\n            NumberOfControls += 1;\r\n            CreateControl();\r\n        }\r\n\r\n        protected void BtnRemoveClick(object sender, EventArgs e)\r\n        {\r\n            NumberOfControls -= 1;\r\n            RemoveLastControl();\r\n        }\r\n    }\r\n}<\/code>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I worked on a project a couple of years ago using dynamic controls in an Asp.Net WebForms environment. Managing dynamic controls can be a real hassle. So when I read Dave Reed&#8217;s great article about Viewstate, I wondered if I could do it better now. I worked up a simple example using no Viewstate where [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,34,18,24],"tags":[82,69,100],"class_list":["post-236","post","type-post","status-publish","format-standard","hentry","category-aspnet","category-microsoft","category-programming","category-web","tag-asp-net","tag-linkedin","tag-webforms"],"_links":{"self":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts\/236","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/comments?post=236"}],"version-history":[{"count":0,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts\/236\/revisions"}],"wp:attachment":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/media?parent=236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/categories?post=236"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/tags?post=236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}