Some time ago, I needed to dynamically create WEB Components during development. I thought it was a trivial matter, but it was easy to do it. There are some minor issues. Next, I will introduce how to dynamically create and use WEB Components Based on my own programs, hoping to provide some help to my friends who do similar jobs.
I. Program ideas
The program consists of three parts:
1. The program determines the number of WEB components to be created based on the data information in XML.
2. dynamically create WEB components.
3. Use dynamically created WEB components.
2 and 3 are the important parts of this article.
Next I will introduce these three parts in combination with program instances (taking c # as an example.
Ii. Read XML files
Reading XML files is detailed in many documents, and I believe many of my friends have mastered the technology. However, to ensure the integrity of the article, I would like to repeat it here. A friend with a deep harmonic flavor can skip this section.
The XML file to be read in my program is shown below:
Config. xml
<? Xml version = "1.0"?>
<Root>
<Nettype> net </Nettype>
<Totalnum> 6 </Totalnum>
<Cells> 2 </Cells>
<IPlink>
<Name> site 1 </Name>
<IP> 192.8.198.1 </IP>
<Sequence> 1 </Sequence>
</IPlink>
<IPlink>
<Name> Site 2 </Name>
<IP> 192.8.198.2 </IP>
<Sequence> 2 </Sequence>
</IPlink>
... ...
</Root>
The Program for reading XML files is as follows:
Protected void readconfig ()
{
Try
{
System. Xml. XmlDocument mXmlDoc = new System. Xml. XmlDocument ();
MXmlDoc. Load (Server. MapPath (configfilepath ));
Nettype = mXmlDoc. selectNodes ("// Root/Nettype") [0]. innerText; totalnum = int. parse (mXmlDoc. selectNodes ("// Root/Totalnum") [0]. innerText );
// Read the number of Columns
Cells = int. Parse (mXmlDoc. SelectNodes ("// Root/Cells") [0]. InnerText );
XmlNodeList mXmlNodes = mXmlDoc. SelectNodes ("// Root/IPlink ");