前段时间笔者在开发中需要动态创建WEB组件,本以为是小事一桩,谁知看时容易做时难。里面还真有些小问题。下面笔者就结合自己的程序来介绍一下如何动态创建并使用WEB组件,希望能给做类似工作的朋友提供一点帮助。
一、程序思路
程序主要分三部分:
1、程序要根据XML中的数据信息确定需要创建的WEB组件的个数。
2、动态创建WEB组件。
3、使用动态创建的WEB组件。
其中2和3是笔者要重点介绍的部分。
下面笔者就按照这三部分结合程序实例(以c#为例)来一一介绍。
二、读取XML文件
读取XML文件在很多的资料中都有详细的说明,而且相信很多朋友都已经很好的掌握了其技术。但为了保证文章的完整性,笔者在这里还是要赘述几句。深谐其味的朋友可以略过此段不看。
笔者程序中要读取的XML文件形如下列:
config.xml<?xml version="1.0"?><Root><Nettype>net</Nettype><Totalnum>6</Totalnum><Cells>2</Cells><IPlink><Name>站点1</Name><IP>192.8.198.1</IP><Sequence>1</Sequence></IPlink><IPlink><Name>站点2</Name><IP>192.8.198.2</IP><Sequence>2</Sequence></IPlink>… …</Root>
读取XML文件的程序如下:
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);//读出列数cells=int.Parse(mXmlDoc.SelectNodes("//Root/Cells")[0].InnerText);XmlNodeList mXmlNodes=mXmlDoc.SelectNodes("//Root/IPlink");foreach(XmlNode IPlinkchildlNode in mXmlNodes){//得到序列号int icount=int.Parse(IPlinkchildlNode.ChildNodes[2].InnerText);//根据序列号,将测量点的名称放入名称数组相应的位置上namestr[icount]=IPlinkchildlNode.ChildNodes[0].InnerText;//根据序列号,将测量点的IP放入IP数组相应的位置上ipstr[icount]=IPlinkchildlNode.ChildNodes[1].InnerText;}}catch{errmessage.InnerHtml="<table align=center><tr><td align=left><font color=red>不能读取配置文件,可能的错误是<br>"+"1、配置文件不存在<br>"+"2、配置文件内容被损坏"+"</font></td></tr></table>";}}
程序中对XML中无子节点的元素如:
<Nettype>net</Nettype>
直接使用如下语句读取。
mXmlDoc.SelectNodes("//Root/Nettype")[0].InnerText;
对于有子节点的元素如:
<IPlink><Name>站点1</Name><IP>192.8.198.1</IP><Sequence>1</Sequence></IPlink>
要使用语句如下来读取。
IPlinkchildlNode.ChildNodes[N].InnerText
其中 ChildNodes[N] 中的[N]为子节点的序号,子节点
<Name>站点1</Name>
的序号应该为[0]。
三、动态创建WEB组件
先来看程序实例:
private void createconfigtable(int totalnum,int[] sequenceint,string[] namestr,string[] ipstr){ //根据得到测量点的总数,动态生成输入框 for(int i=1;i<=totalnum;i++) { //创建表格 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); //创建一行 HtmlTableRow tRow = new HtmlTableRow(); showtable.Rows.Add(tRow); //创建第一列(序号) HtmlTableCell tCell = new HtmlTableCell(); Label sequenceLabel = new Label(); sequenceLabel.ID="sequenceLabel"+i.ToString(); sequenceLabel.Text="序号:"; sequenceLabel.Enabled=true; tCell.Controls.Add(sequenceLabel); tRow.Cells.Add(tCell); //创建第二列 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; //创建第三列(名称) tCell = new HtmlTableCell(); Label nameLabel = new Label(); nameLabel.ID="nameLabel"+i.ToString(); nameLabel.Text="名称:"; nameLabel.Enabled=true; tCell.Controls.Add(nameLabel); tRow.Cells.Add(tCell); //创建第四列 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); //创建第五列(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); //创建第六列 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);… …//创建第五列(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);//创建第六列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);}}
一起学吧部分文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与一起学吧进行文章共享合作。