源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2022-06-20 09:11 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:ASP.NET中根据XML动态创建使用WEB组件
  前段时间笔者在开发中需要动态创建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组件”部分会有较为详细的介绍)
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部