Subscribe

RSS Feed (xml)

Dynamically Adding User Controls to a Web Form

You simply load the User Control into a Control object and add it to the page (or
other control inherited from System.Web.UI.Control). In <script runat=”server”>
or codebehind:
Sub Page_Load(sender as object, e as EventArgs)
Dim sampleControl as Control = LoadControl(“recipe0201vb.ascx”)
Controls.Add(sampleControl)
End Sub

You can also add an @ Reference directive at the top of the page to tell ASP.NET to
dynamically compile and link the control at runtime. In the ASPX page:
<%@ Reference Control=”recipe0201vb.ascx” %>

The LoadControl function takes the virtual path to the User Control (specified as the
parameter) and returns a Control object.This object can then be loaded onto the page
with Controls.Add(control As System.Web.UI.Control).The Controls.Add function
is also a member of any other control that inherits from System.Web.UI.Control.
This technique can allow you to dynamically create your entire Web site.The
IBuySpy portal sample application makes extensive use of this technique.

No comments:

Archives

Variety in the Web World