Preliminary effective control scheme for dynamically generating controls on webpages (I)

Source: Internet
Author: User

Some netizens asked the issue that controls no longer exist when controls are dynamically generated in ASP. NET during callback (see post: ☆★Ask questions about dynamically generated controls! Wait for a reply !! Help !!), This is also a problem I encountered at work. The solution is as follows (the following content is directly copied from QQ chat ):


Haha, that's the problem with the last 20 points. Haha
Stupid snail mail 17:37:18
Stupid snail mail 17:37:33
Your ASP. Net Development basics?

I tried a lot of methods.
When the control is loaded, all the data previously given to it is gone.

I even use Mr Cheng
Then findcontrol finds the control and runs a function to transmit data to it.

I used to learn Win32
ASP. NET just a month ago
I think the language is okay, but ASP is not very good.

A friend said:
I feel like I want to add the control to the page first. It can be done with a delegate.

What do I mean?
Stupid snail mail 17:40:05
Let me tell you a question I encountered at work.
Stupid snail mail 17:40:49
Multiple usercontrols are used on my page. Different usercontrols are displayed in different States based on user operations. Only one usercontrol is displayed at a time.

...... My mom is coming back. I want to cook ~~~~~

Are you there at night?

Or you can leave a message for me.
Stupid snail mail 17:41:38
To put it bluntly, it is a user registration page that writes different pages into different usercontrol and adds them to the page.
Stupid snail mail 17:41:47
I will send it to you in this way.

Oh, I know.
Stupid snail mail 17:41:58
Ask again after you finish reading.

well, let's talk about it in detail.
well, I'll cook first.
stupid snail s' 17:43:24
the usual practice is to add these usercontrols once, set different usercontrol Parameters Based on the steps. However, I feel that such processing is a waste of resources and there is no skill. Therefore, I dynamically add usercontrol to load the control (loadcontrol ).
stupid snail mail 17:44:48
it was successful to go on step by step. However, when I wanted to obtain the usercontrol data, a problem occurred, it is difficult to solve this problem because different locations use different usercontrols. Later, I analyzed the operating mechanism of. NET and found a solution.
stupid snail guard 17:47:17
Add a hidden domain on the page, the ID is hiddentext
defines a usercontrol variable of the usercontrol type on the page (used to place the usercontrol that is added)
define an icurstep of the int type on the page (record the current step)
define a function addmaincontrol on the page, this function adds different usercontrols based on different icurstep values.
stupid snail guard 17:50:29
open the oninit function, at the beginning:
{< br> icurstep = convert. toint32 (request. form [hiddentext. uniqueid]);
}< br> catch
{< br> icurstep = 0; // The default value is 0, step 1
}< br> then addmaincontrol is called to load the usercontrol control.
this ensures that the last output usercontrol will continue to exist when the page is post.
stupid snail mail 17:52:06
the page contains the previous and next buttons. The corresponding click/command event Code is:
icurstep ++
icurstep --
then, addmaincontrol is called.
This ensures the loading of usercontrol in the new step.

Stupid snail mail 17:55:23
Add the prerender event handler in the Code:
Hiddentext. value = icurstep. tostring ();
In this way, the sequence number of the current step is saved to the view data on the page (viewstate cannot be used here to save it, Because viewstate has not loaded data before oninit .)
With the above steps, I have successfully implemented dynamic loading of usercontrol.

Note: The above hidden domain is runat = server.


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: 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.