{"id":41,"date":"2011-10-20T09:40:00","date_gmt":"2011-10-20T09:40:00","guid":{"rendered":"https:\/\/wdev-blog.azurewebsites.net\/index.php\/2011\/10\/20\/loading-a-custom-control-at-runtime\/"},"modified":"2011-10-20T09:40:00","modified_gmt":"2011-10-20T09:40:00","slug":"loading-a-custom-control-at-runtime","status":"publish","type":"post","link":"http:\/\/panahy.nl\/index.php\/2011\/10\/20\/loading-a-custom-control-at-runtime\/","title":{"rendered":"Loading a custom control at runtime"},"content":{"rendered":"<p>You can load a server control to your page using the constructor of the control. For example a Literal control can be loaded as follows:<\/p>\n<pre><code>plhContainer.Controls.Add(<br \/>            new Literal<br \/>            {<br \/>                Text = string.Format(\"{0}.{1}\",<br \/>                    method.ReflectedType.FullName, method.Name)<br \/>            });<br \/><\/code><\/pre>\n<p>When it comes to a custom control you also want the markup code of you custom control to be loaded too. So you need to tell the Page o load it for you:<\/p>\n<pre><code><br \/>var nameBox = (NameBox) Page.LoadControl(\"NameBox.ascx\");<br \/><\/code><\/pre>\n<p>Basically, you could do this at any time, but it is recommanded to do this at <b>Page_Load<\/b> the reason is that <b>this is the best place for the control to restore its state and receive postback events.<\/b> Also the binding will take place after this method. Look at <a href=\"http:\/\/wdevs.blogspot.com\/2011\/10\/asp-page-en-control-lifecycle.html\">my previous post<\/a> for the sequence of the events and method calls when page gets loaded.<br \/>It is also recomannded to set a unique ID to that control if you need to find that later using FindControl, or some one else want to find where you have put it \ud83d\ude42<\/p>\n<p>So, my load method will look like this:<\/p>\n<pre><code><br \/>     private void LoadMyControls()<br \/>     {<br \/>         var nameBox = (NameBox) Page.LoadControl(\"NameBox.ascx\");<br \/>         \/\/ Give the user control a unique name by setting its ID property.<br \/>         \/\/ You can use this information to retrieve a reference to the control<br \/>         \/\/ when you need it with the Page.FindControl() method.<br \/>         nameBox.ID = \"nameBox\"; <br \/>         nameBox.FirstName = \"Asghar\";<br \/>         nameBox.LastName = \"Panahy\";<br \/>         nameBox.ChangeRequest += ChangeName;<br \/>         plhContainer.Controls.Add(nameBox);<br \/>     }<br \/><\/code><\/pre>\n<p>When I call LoadMyControls() in Page_Load method, I see the following sequence in my output:<\/p>\n<pre><code><br \/>STARTING: MCTS._70_515.Resources._Default.Page_Init<br \/>STARTING: MCTS._70_515.Resources._Default.OnInit<br \/>STARTING: MCTS._70_515.Resources._Default.Page_Load<br \/>STARTING: MCTS._70_515.Resources.NameBox.Page_Init<br \/>STARTING: MCTS._70_515.Resources.NameBox.OnInit<br \/>STARTING: MCTS._70_515.Resources._Default.OnLoad<br \/>STARTING: MCTS._70_515.Resources.NameBox.Page_Load<br \/>STARTING: MCTS._70_515.Resources.NameBox.<b><span style=\"color: #674ea7;\">OnDataBinding<\/span><\/b><br \/>STARTING: MCTS._70_515.Resources.NameBox.get_LastName<br \/>STARTING: MCTS._70_515.Resources.NameBox.OnLoad<br \/>STARTING: MCTS._70_515.Resources._Default.OnPreRender<br \/>STARTING: MCTS._70_515.Resources.NameBox.OnPreRender<br \/>STARTING: MCTS._70_515.Resources.NameBox.OnUnload<br \/>STARTING: MCTS._70_515.Resources._Default.OnUnload<br \/><\/code><\/pre>\n<p>Notice that the custom control gets its data binded just after the page has passed Loading which is a good thing.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can load a server control to your page using the constructor of the control. For example a Literal control can be loaded as follows: plhContainer.Controls.Add( new Literal { Text = string.Format(&#8220;{0}.{1}&#8221;, method.ReflectedType.FullName, method.Name) }); When it comes to a custom control you also want the markup code of you custom control to be loaded &hellip; <a href=\"http:\/\/panahy.nl\/index.php\/2011\/10\/20\/loading-a-custom-control-at-runtime\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Loading a custom control at runtime&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[34,36],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false},"uagb_author_info":{"display_name":"Pouya Panahy","author_link":"http:\/\/panahy.nl\/index.php\/author\/pouya\/"},"uagb_comment_info":0,"uagb_excerpt":"You can load a server control to your page using the constructor of the control. For example a Literal control can be loaded as follows: plhContainer.Controls.Add( new Literal { Text = string.Format(\"{0}.{1}\", method.ReflectedType.FullName, method.Name) }); When it comes to a custom control you also want the markup code of you custom control to be loaded&hellip;","_links":{"self":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/41"}],"collection":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/comments?post=41"}],"version-history":[{"count":0,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/41\/revisions"}],"wp:attachment":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/media?parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/categories?post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/tags?post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}