Create a user control (ASCX) in the user control (ASCX)

Source: Internet
Author: User

"I have created two ascx, ascxA, and ascxB
AscxA has a PlaceHold,
A textBox is placed in ascxB.
AscxA dynamically creates five ascxB instances in page_load, but nothing is displayed on the page"



This is a question from Forum users. Insus. NET will give a demo of this question, and share the methods and skills with you.

Create a user control ascx B on the site and pull a TextBox on the control.
Create another user control, ascx A, and pull A Textbox and A button on the user control to allow the user to enter data in the text box and click the button to dynamically generate the ascx B user control, displayed on the page of the ascx A user control.
Create An aspx webpage on the site. Reference the user control ascx to the aspx webpage. Then, pull a button on the aspx webpage. Let the user click this button to obtain the value of the dynamically generated text box and display it on the aspx webpage.

The problem is that ascx A dynamically loads ascx B. It is generated based on the number entered by the user.
On the aspx webpage, how does one obtain the values of dynamically generated text boxes?

Well, complete them step by step with questions.
Create a site. Insus. NET uses. NET 4.0 and C # As the program language.
Create an AscxB. ascx user control:

View Code

 
AscxB. ascx. cs:

Page_Load (View Code

 

Create a user control AscxA. asx on the site. On this control, pull a TextBox, a Button for the ammonium, and a container PlaceHolder. Add the onclick event OnClick = "ButtonGenerate_Click" on the button"

View Code


AscxA. ascx. cs:

Page_Load (ButtonGenerate_Click (View Code

 

The following describes how to dynamically load a user control. In order to allow the user control ascx A to be loaded to ascx B. Here, Insus. NET first creates an interface ):

 

Write the interface, and implement this interface in ascx B:



Let's go back to the user control ascxA cs code page and write the button Click event:

 


In the 29 lines of code, is to determine whether the file box is empty, is not a number.
Code 35th: After the user control ascx B is dynamically loaded, it is converted into the IUserControlable interface. Then add it to the PlaceHolder container.

Now, we create a webpage. aspx. On this webpage, We reference the user control ascxa. We are also pulling a button and a Literal control. The initial state of the button and Literal is hidden, it is mainly used to obtain data and display data.




. Aspx. cs code:

Page_Load (ButtonGetValue_Click (View Code


Now, let's do something else, that is, when the Ascx A user control dynamically generates the Ascx B control, the Button on the webpage will be displayed. If no Button is generated, the webpage Button is hidden.
Because controls are generated on the ascx A user control, the hidden objects are on the webpage. This involves communication and coordination between user controls and webpages.
To reduce the complexity of the program, Insus. NET has to write an interface, which has only one write-only attribute.



After writing the interface, we implement this interface on the webpage. aspx. cs. To put it bluntly, the button of a webpage only accepts display and hide. It cannot be used to determine the display and hide of the webpage.


Who controls display and hiding? As mentioned earlier, after the dynamic generation of ascx B by the user control ascx A, the Button of this webpage is displayed. Therefore, add the following code to the control generated by the user control ascx:



I have some doubts. How can I convert a webpage into an interface? Because we have implemented the IShowable interface on the webpage.

OK. Let's go back to the webpage cs and prepare to write the click Event of the button to get the data. However, it is A little difficult to get the data, because the dynamically generated controls are all presented in the user control ascx A, and each rendered text box is from ascx B.
On the webpage, how does one obtain the PlaceHolder container of ascx A of the Russian user control? Insus. NET only uses interfaces. Therefore, another interface is written to allow webpage. aspx. cs to read the PlaceHolder of Ascx A of the user control.



Let's go to the user control ascx A to use this interface:



In this way, we can get the container from the ammonium button that obtains the value of the webpage. aspx. cs. In addition, the container may generate more than one ascx B user control multiple times based on the user's needs. How do we know which TextBox is a TextBox?
Write an interface,



After writing the interface, go to the ascx B user control to implement this interface:



So far, we can go to the webpage code to write the event of getting the value from the Click of the button:


In the code above, #0 is used to obtain the placeholder container.
#1 is all text boxes in the foreach container.
#2 is the value of the text box.

So far, the demo is complete. However, let's make a final demonstration:



Demo source code:
Http://download.cnblogs.com/insus/ASPDOTNET/Ascx_load_ascx_page_getvalue.rar

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.