The Web page dynamically loads the UserControl and invokes a method in the user control to initialize the control

Source: Internet
Author: User

1,html page

Head registration:

<%@ Register SRC="~/worklog/worklognewv1/usercontrol/ceshibu.ascx"TagPrefix="UserControl"TagName="Ceshibu"%><%@ Register SRC="~/worklog/worklognewv1/usercontrol/kaifabu.ascx"TagPrefix="UserControl"TagName="Kaifabu"%>

Control Placement:

<id= "Controlspan"  runat=server>     </  div>

2,c# Backstage

 /// <summary>    ///determine which features in a user control are displayed based on items and classifications/// </summary>    /// <param name= "STRFL" ></param>    /// <param name= "STRSX" ></param>    Private voidSetusercontrolvisible (stringSTRFL,stringSTRSX) {        if(viewstate["Rzdeptname"]. ToString () = ="Development Department")        {
User Control path UserControl kaifabucontrol= (UserControl) Page.LoadControl ("Usercontrol/kaifabu.ascx"); Type TC=Kaifabucontrol.gettype ();
The initialization method to invoke in the user control System.Reflection.MethodInfo m= TC. GetMethod ("OnStart");
Calling the parameters to be passed by the initialization methodObject[] Bjparas =New Object[1]; bjparas[0] =STRSX; M.invoke (Kaifabucontrol, Bjparas); Controlspan. Controls.clear (); Controlspan. Controls.Add (Kaifabucontrol); } if(viewstate["Rzdeptname"]. ToString () = ="Testing Department") {UserControl Kaifabucontrol= (UserControl) Page.LoadControl ("Usercontrol/ceshibu.ascx"); Type TC=Kaifabucontrol.gettype (); System.Reflection.MethodInfo m= TC. GetMethod ("OnStart"); Object[] Bjparas =New Object[1]; bjparas[0] =STRSX; M.invoke (Kaifabucontrol, Bjparas); Controlspan. Controls.clear (); Controlspan. Controls.Add (Kaifabucontrol); } }

Dynamically changing STRFL,STRSX on a page can dynamically load different user controls or load different elements in a user control.

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.