After the custom control is dynamically loaded in. net, the value and function call method are passed.

Source: Internet
Author: User

Custom User Controls can be directly introduced on the page and dynamically loaded for use:

1. You can directly reference it in the aspx file on the page, and call its public attributes and methods through the control ID in its. CS file:

Eg:

Custom Control ityouhuictl:

Ityouhuictl. ascx. CS:

Define attributes:

 
Private string URL; Public String URL {get {return URL;} set {url = value ;}} Public String seturl (string urlinput, string para2) // set multiple variables through function call {url = urlinput ;...}

Page default. aspx:

<% @ Register src = "/controls/ityouhuictl. ascx "tagname =" ityouhuictl "tagprefix =" WFO "%> <WFO: ityouhuictlid =" YH "runat =" server "/>

Default. aspx. CS:

YH. url = "http://www.ityouhui.com"; // reference directly

2. The function call method is introduced by dynamically loading custom controls:

For function calls:

Eg:

Default. aspx:

<Asp: Panel id = "extpl1" runat = "server" visible = "false"/>

Daefault. aspx. CS:

Control C = page. loadcontrol (request. applicationpath + "/controls/ityouhuictl. ascx "); // dynamically load the Control Type TC = C. getType (); system. reflection. methodinfo M = tc. getmethod ("seturl"); // XX is the control's function object [] bjparas = new object [2]; bjparas [0] = "http://www.ityouhui.com/channel/notebook?#//the parameters bjparas [1] =" seller "; M. invoke (C, bjparas); // call extpl1.controls. add (c); // Add the control to the Panel placeholder

3. dynamically load custom controls and call properties:

First, declare the class name in the custom control:

Eg:

You need to add the classname attribute to the custom control ityouhuictl:

Ityouhuictl. ascx:

<% @ Control Language = "C #" autoeventwireup = "true" codefile = "ityouhuictl. ascx. cs"Classname= "Ityouhuictl" inherits = "controls_ityouhuictl" %>

Register in default. aspx on the reference page:

<% @ Register src = "/controls/ityouhuictl. ascx" tagname = "ityouhuictl" tagprefix = "WFO" %>

In default. aspx. CS, you can directly use:

 
ASP. ityouhuictl C; C = (Asp. ityouhuictl) (page. loadcontrol (request. applicationpath + "/controls/ityouhuictl. ascx "); extpl1.controls. add (c); C. url = "http://www.ityouhui.com/channel/camera?#//direct call! // Similarly, functions can be called here, which is simpler

Note: Before ityouhuictl, it is usually ASP. of course, different configurations may vary. If you do not know, you can directly enter the control name ityouhuictl at the call after completing the previous steps and move the mouse up, vs will prompt you to add a namespace and add it;

Over!

Org: http://blog.donews.com/me1105/archive/2011/05/15/154.aspx

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.