Method 1: drag-and-drop User Controls
<! -- This Is An ASPX page -->
.....
<% @ Register src = "testcontrol. ascx" tagname = "testcontrol" tagprefix = "mycontrol" %>
.....
<Mycontrol: testcontrol id = "testcontrol1" runat = "server"/>
.....
If the testcontrol. ascx control contains the dosomething method and the access level is public, we can directly call the method in the CS Code as follows:
// CS code file
....
Testcontrol1.dosomething ();
....
Method 2: Dynamically Loaded User Controls
For User Controls Dynamically Loaded using the page. loadcontrol () method, we can do this:
// CS File
.....
ASP. testcontrol_ascx testcontrol1 =
(Asp. testcontrol_ascx) page. loadcontrol ("testcontrol. ascx ");
Testcontrol1.dosomething ();
Method 3: dynamically load controls using the page. Pase Method
For User Controls Dynamically Loaded using the page. parsecontrol () method, we can do this:
Control control = This. page. parsecontrol (@ "<% @ register src =" "testcontrol. ascx "" tagname = "" testcontrol "" tagprefix = "" mycontrol "" %> <mycontrol: testcontrol id = "" testcontrol1 runat = "" server ""/> ");
This. Page. Controls. Add (control );