Dynamically create an ASP. Net Control and maintain its status analysis and implementation

Source: Internet
Author: User

Dynamically create the Asp.net control and save its status, which is summarized as follows:
1: Background Knowledge: You need to know the lifecycle of the page.
2: method summary:
1>
In the declaration area of the page code file (the label control is used as an example here ):
Label x = new label ();
The code in the pageload event is:
Protected void page_load (Object sender, eventargs E)
{
This. Page. Controls. Add (X );
}
When the X value needs to be set on the page, write: X. Text = "Zzz"; and so on. In this way, no matter how the page is refreshed, You can dynamically create controls and save the status.

2>
Idea: dynamically Add a control to an existing control or control container on the page, and then set its status as needed.
For example:
<Asp: repeater id = "repeater1" runat = "server" onitemdatabound = "repeaterincluitemdatabound">
<Itemtemplate>
<Asp: literal id = "TR" runat = "server"> </ASP: literal>
</Itemtemplate>
</ASP: repeater>
Write :( E. Item. findcontrol ("ds") as literal). Text = "";

The two methods are similar: Once a control is added to the Control tree of the page, the trackviewstate () method tracks its status. This method is automatically triggered as long as the control is added to the Control tree.

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.