Asp. NET to create and use Web Components

Source: Internet
Author: User
Asp.net|web| Create a period of time before the author in the development of the need to dynamically create Web Components, this thought is a trivial matter, who knows it easy to do when the difficult. 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, 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 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].



Iii. dynamic creation of 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)


Iv. using dynamically created Web Components

If you want to use this component after you have created the Web Component dynamically, 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 object of type control, using (Label) to cast the type to the label type. Then you can do whatever you need to do with this object you want to operate. such as changing the properties of a component, and so on.

A special note is that using the above statement to use a dynamically created Web component has the premise that the page cannot be refreshed. If the page is refreshed and you do not re-create the component after the page has been refreshed. Use the above statement in your program to use a dynamically created Web component, and you will get an error while the program is running. This error occurs because the dynamically created Web Component does not exist after the page has been refreshed. There is such a problem in the author's procedure. Because the author of the process of thinking is

1. Read XML file

2. Dynamically create Web Components based on read information and assign values to their text properties

3. Allows users to modify the text properties of a dynamically created Web component.

4, after the user submits the modification, wants to write the modified attribute to the XML file

However, when the user submits the change, the page is refreshed. So how do you get the modified attributes?

The author uses the following method in the program:

private void Summitbutton_click (object sender, System.EventArgs e)
{
... ...
Writes the measurement point information that the user fills in 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 for the Web component, so you can still get the modified information.

If you want to still display the dynamically created Web component after the page is refreshed, you need to invoke the function that created the Web Component again in Page_Load (object sender, System.EventArgs e) and use Request.form[id] Assign the modified value to the corresponding component. Keep in mind that the function that creates the Web component is placed in the IF (IsPostBack) {}.

V. Concluding remarks

This paper presents a method of dynamically creating and using Web Components with examples. After reading this article, I believe that friends who still have questions about dynamically creating and using Web Components should have no problem. This example is debugged under. NET 2003+windows.


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.