I am not sure I gave this post an appropriate title, but it works for now.

The Problem:
ASP.Net gives a developer the opportunity to programmatically add controls to a web form using ParentControl.Controls.Add(new Control()); However, these controls are not persisted in any way thus having to be recreated for each subsequent request.

What I found:
On the page I have a PlaceHolder. Depending on the dynamic data at the time of processing, one of 10 “layout” UserControls need to be added to the PlaceHoder. And inside each of those “layouts” will be 2 to 4 “content” UserControls with dynamic data based on the data and properties set by the “layout” UserControl. While stepping through code, durring a PostBack with user data, I was able to see that properties and server controls were being properly set in both UserControls, but when the page was actually rendered to the browser the data displayed was the old data prior to the user making changes.

I tested this and retested this and it baffled me for quite some time. I thought maybe it was an issue of the beta version of Atlas I was using, so I removed the UpdatePanel surrounding all this and problem still existed. After much searching I began to learn that this is a problem (bug) with dynamically added UserControls and all the server controls and user controls within.

Durring my search I found someone who developed a workaround, or a better improved PlaceHolder to insure the proper handling of the data within the added UserControls. Though I did not use this, I suspect it would work, but instead, I found a different way to dynamically determin and build 1 of 10 different layouts.

What I Did:
I am not providing any real answer here, but just confirmation that this problem does indeed exist. So either find a different way or try out this guys workaround.

Instead of using 10 different layout UserControls (10 ascs files with different table formatting and each including 4 instances of a “content” UserControl (these 10 ascx fils all included the same layout.cs code behind file). I put logic in my business layer to determin the layout to use and send back a section of HTML and place the “content” UserControls with in it. My 10 to 1 layout page lookes like this:

<%= GetFrameTablePart(“a”) %>
<uc1:ucObjContent ID=”ucObjContent1″ runat=”server” />
<%= GetFrameTablePart(“b”) %>
<uc1:ucObjContent ID=”ucObjContent2″ runat=”server” />
<%= GetFrameTablePart(“c”) %>
<uc1:ucObjContent ID=”ucObjContent3″ runat=”server”/>
<%= GetFrameTablePart(“d”) %>
<uc1:ucObjContent ID=”ucObjContent4″ runat=”server”/>
<%= GetFrameTablePart(“e”) %>

Advertisements