当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET中根据XML动态创建使用WEB组件

ASP.NET
Asp.net利用JQuery弹出层加载数据代码
asp.net dataview做无限极分类的又一用法
asp.net ckeditor编辑器的使用方法
告别ADO.NET实现应用系统无缝切换的烦恼(总结篇)
asp.net 实现动态显示当前时间(不用javascript不考虑开销)
.net动态显示当前时间(客户端javascript)
asp.net 结合YUI 3.0小示例
asp.net 取消缓存相关问题说明
asp.net 计划任务管理程序实现,多线程任务加载
ASP.NET 跨页面传值方法
asp.net中url地址传送中文参数时的两种解决方案
Asp.net 菜单控件简洁版
asp.net jQuery Ajax用户登录功能的实现
asp.net SharpZipLib的压缩与解压问题
asp.net url重写后页面回传问题
asp.net与Discuz!NT整合集成实例教程
Discuz!NT 3与asp.net 整合的实例教程
测试控制台使用方法
.net 动态标题实现方法
asp.net *.ashx类型的文件使用说明

ASP.NET中根据XML动态创建使用WEB组件


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 64 ::
收藏到网摘: n/a

前段时间笔者在开发中需要动态创建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);
}
}
程序中的myPlaceHolder 是 System.Web.UI.WebControls.PlaceHolder 组件,使用该组件的HTML语法如下:
… …
<tr>
<td>
<asp:PlaceHolder id="myPlaceHolder" runat="server"></asp:PlaceHolder>
</td>
</tr>
… …
使用该组件的目的是为了定位动态创建的表格。该组件在页面上的位置即为动态创建的表格的位置。
程序中另外一个要说明的地方是动态创建的组件的ID的设定。组件的ID的设定要注意两点:
1、ID号不能重复
2、要便于在程序中使用。因为要在程序中使用动态创建的组件,要通过该组件的ID来查找。(关于这一点,在“使用动态创建的WEB组件”部分会有较为详细的介绍)