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 combine my ownProgramThis section describes how to dynamically create and use Web Components, hoping to provide some help to 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. But to ensureArticleIntegrity, I want to repeat a few words 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 the program. 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.