[null, {"document_url": "http://bresleveloper.blogspot.com/2015/02/server-control-with-child-control.html", "unformatted_src": "http://2.bp.blogspot.com/-sqWurTJJfyo/Vq9bW-kGghI/AAAAAAAAAR4/nnt6f7BZbow/s1600/dcbx.png", "src": "http://2.bp.blogspot.com/-sqWurTJJfyo/Vq9bW-kGghI/AAAAAAAAAR4/nnt6f7BZbow/s1600/dcbx.png", "formatted_filename": "dcbx"}, null, {"document_url": "http://bresleveloper.blogspot.com/2015/02/server-control-with-child-control.html", "unformatted_src": "//2.bp.blogspot.com/-XdMxqTuqKnk/Wua0DP_h20I/AAAAAAAAAqQ/tXrRfJKLaMIAKnV8xH0LpO9nQrpkvd6gQCK4BGAYYCw/s120-pf/STmk7.png", "src": "http://2.bp.blogspot.com/-XdMxqTuqKnk/Wua0DP_h20I/AAAAAAAAAqQ/tXrRfJKLaMIAKnV8xH0LpO9nQrpkvd6gQCK4BGAYYCw/s120-pf/STmk7.png", "formatted_filename": "STmk", "alt_text": "My photo", "rendered_width": 120, "rendered_height": 120}, null]

Server Control with Child Control, dynamic created, retain control

public class RelatedPublicationFilesSave : Control
protected HiddenField _test;

protected override void OnInit(EventArgs e)
_test= new HiddenField();
_test.ID = "_test";
protected override void OnLoad(EventArgs e)
_RelatedPublicationFilesSave =
this.FindControlById("_test") as HiddenField;
Log.Debug("_test.Value: " + _test.Value);

the point is that OnInit happens before the page parses itself, so creating the control there without passing any value to it with the same ID will eventually render the same "name" property (UniqueID) and thus assigning the right values from the request.

lets do another example, answering this, where we want to add a table with dynamic Checkboxes count.

we start by creating a server control, creating and declaring all controls in the OnInit, rendering the HTML in the RenderControl

namespace WebApplication3
public class DynamicCbs : Control
public int CtrlsCount { get; set; }
public List<CheckBox> lstCheckBoxs;

/// decleration of controls must be in the OnInit since the next stage of the page life cycle is to connect whatever came back from the client to the server
lstCheckBoxs = new List<CheckBox>();
for (int i = 0; i < CtrlsCount; i++)
string id = "DynamicCbs" + i;
CheckBox cbx = new CheckBox()
ID = id,
Text = "i am " + id
//add controls to control tree

/// here you must build ur html
public override void RenderControl(HtmlTextWriter writer)
foreach (var cbx in lstCheckBoxs)
in the .aspx file

<%@ Register TagPrefix="my" Namespace="WebApplication3" Assembly="WebApplication3" %>
<my:DynamicCbs ID="Dynamicus" runat="server" CtrlsCount="10"></my:DynamicCbs>
<asp:Button runat="server" Text="Button" />
<h1>true controls:</h1>
<div runat="server" id="txt"></div>

in our .aspx.cs Page_Load

foreach (var cbx in Dynamicus.lstCheckBoxs)
if (cbx.Checked) {
txt.InnerHtml += cbx.ID + "<br/>"; dynamic created control server control








