ASP.NET creates and uses web components according to XML (3)

zhaozj2021-02-16  50

ASP.NET creates a web component according to XML dynamics (3)

Author: Li Tieshuai

Fourth, using a dynamically created web component

If you use this component after you create a web component, you can use the following statement.

String sequencelabelid = "sequencedatatb" iCount.toString ();

Label sequencelabel = (label) MyPlaceHolder.FindControl (SequenceLabelid);

SEQUENCELABEL.TEXT = "..."

...

Where sequencelabelid is the ID number of the component to use, the MyplaceHolder.FindControl () method returns the type of Control, using (Label) to convert this type to the label type. Then you can do anything to do anything according to your needs. If you change the properties of the component, etc..

It is necessary to note that it is necessary to use the above statement to use a dynamically created web component, that is, the page cannot be refreshed. If the page is refreshed, and you don't recreate the component after the page is refreshed. Use the above statements in the program to use dynamically created web components, get an error when the program is running. The incorrect generation is because the dynamically created web components do not exist after the page is refreshed. There is such a problem in the author's program. Because the idea of ​​the author is

1, read the XML file

2, dynamically create a web component based on the read information, and assign a value for its text attribute

3. Allow users to modify the text properties of dynamically create a web component.

4. When the user submits the modification, write the modified attribute into the XML file.

But when the user is submitted, the page will be refreshed. So how should I get a modified property?

The author uses the following method in the program:

Private Void SummitButton_Click (Object Sender, System.Eventargs E)

{

...

// Write the measurement point information filled in the user to the array

For (int icount = 1; icount <= int.parse (ViewState ["TotalNum"]. TOSTRING ()); ICOUNT )

{

...

String Nametbid = "Nametb" iCount.toString ();

String Nametbtext = Request.form [NametBid] .tostring ();

NameStr [iCount] = NametbText;

}

...

}

Where NametBid is the ID number of the web component to use. When the page is refreshed, although the dynamically created web component does not exist, the Request object has saved the information of the web component, so the modified information can still be obtained.

If you want to display a dynamically created web component after the page is refreshed, then you need to do it again in Page_Load (Object Sender, System.EventArgs E), and then call the creation of the web component again, and use the request.form [id] The corresponding component assigns a modified value. Keep in mind that a function of creating a web component is placed in if (isPostback) {}.

Five, conclusion

This paper gives a method of dynamically created and using a web component in conjunction with an example. After reading this article, I believe that friends who have doubtful to dynamically create and use Web components should have no problem. This example is debugged under .NET 2003 Windows 2000 Server.

mine

Email

address is

Chief_marshal@tom.com

转载请注明原文地址:https://www.9cbs.com/read-21994.html

New Post(0)