Three methods for ASP. net2.0 to load User Controls

Source: Internet
Author: User



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 );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.