Dynamic creation and use of WEB Components Based on XML in ASP. NET

Source: Internet
Author: User

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 ");

Foreach (XmlNode IPlinkchildlNode in mXmlNodes)

{

// Obtain the serial number

Int icount = int. Parse (IPlinkchildlNode. ChildNodes [2]. InnerText );

// Add the measurement point name to the corresponding position of the name Array Based on the serial number

Namestr [icount] = IPlinkchildlNode. ChildNodes [0]. InnerText;

// Add the IP address of the measurement point to the corresponding position of the IP Array Based on the serial number.

Ipstr [icount] = IPlinkchildlNode. ChildNodes [1]. InnerText;

}

}

Catch

{

Errmessage. InnerHtml = "<table align = center> <tr>

<Td align = left> <font color = red> the configuration file cannot be read. The possible error is <br> "+" 1. The configuration file does not exist <br> "+

"2. The configuration file content is corrupted" +

"</Font> </td> </tr> </table> ";

}

}
Elements in the program that have no child nodes in XML are as follows:

<Nettype> net </Nettype>
Use the following statement to read data.

MXmlDoc. SelectNodes ("// Root/Nettype") [0]. InnerText;
For elements with subnodes, such:

<IPlink>

<Name> site 1 </Name>

<IP> 192.8.198.1 </IP>

<Sequence> 1 </Sequence>

</IPlink>
If you want to use a statement, read it.

IPlinkchildlNode. ChildNodes [N]. InnerText
[N] In ChildNodes [N] is the serial number of the subnode.

<Name> site 1 </Name>
The serial number must be [0].

3. dynamically create WEB components.

Let's first look at the program instance:

Private void createconfigtable (int totalnum, int [] sequenceint, string [] namestr, string [] ipstr)

{

// Dynamically generate an input box based on the total number of measurement points

For (int I = 1; I <= totalnum; I ++)

{

// Create a table

HtmlTable showtable = new HtmlTable ();

Showtable. Border = 0;

Showtable. ID = "showtable" + I. ToString ();

Showtable. BorderColor = "#000000 ";

Showtable. CellPadding = 4;

Showtable. CellSpacing = 4;

Showtable. Align = "center ";

MyPlaceHolder. Controls. Add (showtable );

// Create a row

HtmlTableRow tRow = new HtmlTableRow ();

Showtable. Rows. Add (tRow );

// Create the first column (sequence number)

HtmlTableCell tCell = new HtmlTableCell ();

Label sequenceLabel = new Label ();

SequenceLabel. ID = "sequenceLabel" + I. ToString ();

SequenceLabel. Text = "No :";

SequenceLabel. Enabled = true;

TCell. Controls. Add (sequenceLabel );

TRow. Cells. Add (tCell );

// Create the second column

TCell = new HtmlTableCell ();

SequencedataTB = new TextBox ();

SequencedataTB. ID = "sequencedataTB" + I. ToString ();

SequencedataTB. Text = I. ToString ();

SequencedataTB. Width = 30;

SequencedataTB. Text = sequenceint [I]. ToString ();

SequencedataTB. ReadOnly = false;

// Create the third column (name)

TCell = new HtmlTableCell ();

Label nameLabel = new Label ();

NameLabel. ID = "nameLabel" + I. ToString ();

NameLabel. Text = "name :";

NameLabel. Enabled = true;

TCell. Controls. Add (nameLabel );

TRow. Cells. Add (tCell );

// Create the fourth column

TCell = new HtmlTableCell ();

NameTB = new TextBox ();

NameTB. ID = "nameTB" + I. ToString ();

NameTB. Width = 120;

NameTB. Text = namestr [I];

NameTB. MaxLength = 50;

TCell. Controls. Add (nameTB );

TRow. Cells. Add (tCell );

// Create the Fifth Column (IP)

TCell = new HtmlTableCell ();

Label ipLabel = new Label ();

IpLabel. ID = "ipLabel" + I. ToString ();

IpLabel. Text = "IP :";

IpLabel. Enabled = true;

TCell. Controls. Add (ipLabel );

TRow. Cells. Add (tCell );

// Create column 6

TCell = new HtmlTableCell ();

IpTB = new TextBox ();

IpTB. ID = "ipTB" + I. ToString ();

Ipterabytes. Width = 120;

IpTB. Text = ipstr [I];

IpTB. MaxLength = 15;

TCell. Controls. Add (ipTB );

TRow. Cells. Add (tCell );

}

}

TCell. Controls. Add (sequencedataTB );

TRow. Cells. Add (tCell );

... ...

// Create the Fifth Column (IP)

TCell = new HtmlTableCell ();

Label ipLabel = new Label ();

IpLabel. ID = "ipLabel" + I. ToString ();

IpLabel. Text = "IP :";

IpLabel. Enabled = true;

TCell. Controls. Add (ipLabel );

TRow. Cells. Add (tCell );

// Create column 6

TCell = new HtmlTableCell ();

IpTB = new TextBox ();

IpTB. ID = "ipTB" + I. ToString ();

Ipterabytes. Width = 120;

IpTB. Text = ipstr [I];

IpTB. MaxLength = 15;

TCell. Controls. Add (ipTB );

TRow. Cells. Add (tCell );

}

}

MyPlaceHolder in the program is the System. Web. UI. WebControls. PlaceHolder component. The HTML Syntax of this component is as follows:

... ...

<Tr>

<Td>

<Asp: PlaceHolder id = "myPlaceHolder" runat = "server"> </asp: PlaceHolder>

</Td>

</Tr>

... ...

This component is used to locate dynamically created tables. The position of this component on the page is the position of the dynamically created table.

Another note in the program is to set the ID of the dynamically created component. Pay attention to the following two points for setting the component ID:

1. the ID number cannot be repeated.

2. It should be easy to use in programs. Because you want to use a dynamically created component in the program, you need to find it by the component ID. (For details about this, refer to "using dynamically created WEB components)

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.