Some time ago the author in the development of the need to dynamically create Web Components, this thought is a trivial matter, who knows that easy to do when it is difficult to do. There's really a little problem in there. The following is a combination of my own program to introduce how to create and use the Web Components dynamically, I hope to do a similar job for friends to provide a little help.
First, the process of thinking
The procedure consists of three main parts:
1, the program to determine the number of Web components that need to be created according to the data information in the XML.
2, the dynamic creation of Web Components.
3. Use dynamically created Web Components.
2 and 3 of them are the part that I want to focus on.
The following author on the basis of these three parts of the combined program example (C # as an example) to introduce.
Second, read XML file
Read the XML file in a lot of information are detailed description, and believe that many friends have a good grasp of their technology. But in order to ensure the integrity of the article, I still have to repeat a few words here. A friend who has a deep taste can skip this paragraph without looking.
The XML file to be read in the author's program is as follows:
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 reading the XML file 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 column number
Cells=int. Parse (Mxmldoc.selectnodes ("//root/cells") [0]. InnerText);
XmlNodeList mxmlnodes=mxmldoc.selectnodes ("//root/iplink");
foreach (XmlNode iplinkchildlnode in Mxmlnodes)
{
Get serial number
int Icount=int. Parse (Iplinkchildlnode.childnodes[2]. InnerText);
According to the serial number, place the name of the measurement point in the appropriate location on the name array
Namestr[icount]=iplinkchildlnode.childnodes[0]. InnerText;
The IP of the measurement point is placed in the appropriate position on the IP array according to the serial number
IPSTR[ICOUNT]=IPLINKCHILDLNODE.CHILDNODES[1]. InnerText;
}
}
Catch
{
Errmessage. Innerhtml= "<table align=center><tr>
<TD Align=left><font color=red> can not read the configuration file, possible error is <br> "+" 1, configuration file does not exist <br> "+
"2, the contents of the configuration file is corrupted" +
"</font></td></tr></table>";
}
}
Elements in the program that have no child nodes in the XML are as follows:
<Nettype>net</Nettype>
Read directly using the following statement.
Mxmldoc.selectnodes ("//root/nettype") [0]. InnerText;
For elements that have child nodes such as:
<IPlink>
<Name> site 1</name>
<IP>192.8.198.1</IP>
<Sequence>1</Sequence>
</IPlink>
To use the statement as read down.
Iplinkchildlnode.childnodes[n]. InnerText
where [N] in childnodes[n] is the ordinal of a child node, a child node
<Name> site 1</name>
The serial number should be [0].
Third, dynamically create Web Components.
First look at the program example:
private void createconfigtable (int totalnum,int[] sequenceint,string[] namestr,string[] ipstr)
{
Dynamically generate an input box based on the total number of points being measured
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 first column (ordinal)
HtmlTableCell Tcell = new HtmlTableCell ();
Label Sequencelabel = new label ();
Sequencelabel.id= "Sequencelabel" +i.tostring ();
Sequencelabel.text= "serial number:";
Sequencelabel.enabled=true;
TCELL.CONTROLS.ADD (Sequencelabel);
TROW.CELLS.ADD (Tcell);
Create a 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 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 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 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 Sixth Column
Tcell = new HtmlTableCell ();
Iptb=new TextBox ();
Iptb.id= "IPTB" +i.tostring ();
iptb.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 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 Sixth Column
Tcell = new HtmlTableCell ();
Iptb=new TextBox ();
Iptb.id= "IPTB" +i.tostring ();
iptb.width=120;
Iptb.text=ipstr[i];
iptb.maxlength=15;
TCELL.CONTROLS.ADD (IPTB);
TROW.CELLS.ADD (Tcell);
}
}
The Myplaceholder in the program is the System.Web.UI.WebControls.PlaceHolder component, and the HTML syntax for using the component is as follows:
... ...
<tr>
<td>
<asp:placeholder id= "Myplaceholder" runat= "Server" ></asp:PlaceHolder>
</td>
</tr>
... ...
The purpose of using this component is to locate dynamically created tables. The location of the component on the page is the location of the dynamically created table.
Another place to be described in the program is the setting of the ID of the dynamically created component. Set the ID of the component to note two points:
1, ID number can not be repeated
2, to facilitate the use of the program. Because you want to use dynamically created components in your program, you are looking through the component's ID. (In this connection, the "use of dynamically created Web Components" section is described in more detail)