ASP.NET中根據(jù)XML動(dòng)態(tài)創(chuàng)建使用WEB組件
前段時(shí)間筆者在開發(fā)中需要?jiǎng)討B(tài)創(chuàng)建WEB組件,本以為是小事一樁,誰知看時(shí)容易做時(shí)難。里面還真有些小問題。下面筆者就結(jié)合自己的程序來介紹一下如何動(dòng)態(tài)創(chuàng)建并使用WEB組件,希望能給做類似工作的朋友提供一點(diǎn)幫助。
一、程序思路
程序主要分三部分:
1、程序要根據(jù)XML中的數(shù)據(jù)信息確定需要?jiǎng)?chuàng)建的WEB組件的個(gè)數(shù)。
2、動(dòng)態(tài)創(chuàng)建WEB組件。
3、使用動(dòng)態(tài)創(chuàng)建的WEB組件。
其中2和3是筆者要重點(diǎn)介紹的部分。
下面筆者就按照這三部分結(jié)合程序?qū)嵗ㄒ詂#為例)來一一介紹。
二、讀取XML文件
讀取XML文件在很多的資料中都有詳細(xì)的說明,而且相信很多朋友都已經(jīng)很好的掌握了其技術(shù)。但為了保證文章的完整性,筆者在這里還是要贅述幾句。深諧其味的朋友可以略過此段不看。
筆者程序中要讀取的XML文件形如下列:
config.xml
<?xml version="1.0"?>
<Root>
<Nettype>net</Nettype>
<Totalnum>6</Totalnum>
<Cells>2</Cells>
<IPlink>
<Name>站點(diǎn)1</Name>
<IP>192.8.198.1</IP>
<Sequence>1</Sequence>
</IPlink>
<IPlink>
<Name>站點(diǎn)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("http://Root/Nettype")[0].InnerText; totalnum=int.Parse(mXmlDoc.SelectNodes("http://Root/Totalnum")[0].InnerText);
//讀出列數(shù)
cells=int.Parse(mXmlDoc.SelectNodes("http://Root/Cells")[0].InnerText);
XmlNodeList mXmlNodes=mXmlDoc.SelectNodes("http://Root/IPlink");
foreach(XmlNode IPlinkchildlNode in mXmlNodes)
{
//得到序列號(hào)
int icount=int.Parse(IPlinkchildlNode.ChildNodes[2].InnerText);
//根據(jù)序列號(hào),將測(cè)量點(diǎn)的名稱放入名稱數(shù)組相應(yīng)的位置上
namestr[icount]=IPlinkchildlNode.ChildNodes[0].InnerText;
//根據(jù)序列號(hào),將測(cè)量點(diǎn)的IP放入IP數(shù)組相應(yīng)的位置上
ipstr[icount]=IPlinkchildlNode.ChildNodes[1].InnerText;
}
}
catch
{
errmessage.InnerHtml="<table align=center><tr>
<td align=left><font color=red>不能讀取配置文件,可能的錯(cuò)誤是<br>"+"1、配置文件不存在<br>"+
"2、配置文件內(nèi)容被損壞"+
"</font></td></tr></table>";
}
}
程序中對(duì)XML中無子節(jié)點(diǎn)的元素如:
<Nettype>net</Nettype>
直接使用如下語句讀取。
mXmlDoc.SelectNodes("http://Root/Nettype")[0].InnerText;
對(duì)于有子節(jié)點(diǎn)的元素如:
<IPlink>
<Name>站點(diǎn)1</Name>
<IP>192.8.198.1</IP>
<Sequence>1</Sequence>
</IPlink>
要使用語句如下來讀取。
IPlinkchildlNode.ChildNodes[N].InnerText
其中 ChildNodes[N] 中的[N]為子節(jié)點(diǎn)的序號(hào),子節(jié)點(diǎn)
<Name>站點(diǎn)1</Name>
的序號(hào)應(yīng)該為[0]。
三、動(dòng)態(tài)創(chuàng)建WEB組件。
先來看程序?qū)嵗?
private void createconfigtable(int totalnum,int[] sequenceint,string[] namestr,string[] ipstr)
{
//根據(jù)得到測(cè)量點(diǎn)的總數(shù),動(dòng)態(tài)生成輸入框
for(int i=1;i<=totalnum;i++)
{
//創(chuàng)建表格
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);
//創(chuàng)建一行
HtmlTableRow tRow = new HtmlTableRow();
showtable.Rows.Add(tRow);
//創(chuàng)建第一列(序號(hào))
HtmlTableCell tCell = new HtmlTableCell();
Label sequenceLabel = new Label();
sequenceLabel.ID="sequenceLabel"+i.ToString();
sequenceLabel.Text="序號(hào):";
sequenceLabel.Enabled=true;
tCell.Controls.Add(sequenceLabel);
tRow.Cells.Add(tCell);
//創(chuàng)建第二列
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;
//創(chuàng)建第三列(名稱)
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);
//創(chuàng)建第四列
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);
//創(chuàng)建第五列(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);
//創(chuàng)建第六列
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);
… …
//創(chuàng)建第五列(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);
//創(chuàng)建第六列
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>
… …
使用該組件的目的是為了定位動(dòng)態(tài)創(chuàng)建的表格。該組件在頁面上的位置即為動(dòng)態(tài)創(chuàng)建的表格的位置。
程序中另外一個(gè)要說明的地方是動(dòng)態(tài)創(chuàng)建的組件的ID的設(shè)定。組件的ID的設(shè)定要注意兩點(diǎn):
1、ID號(hào)不能重復(fù)
2、要便于在程序中使用。因?yàn)橐诔绦蛑惺褂脛?dòng)態(tài)創(chuàng)建的組件,要通過該組件的ID來查找。(關(guān)于這一點(diǎn),在“使用動(dòng)態(tài)創(chuàng)建的WEB組件”部分會(huì)有較為詳細(xì)的介紹)
- Asp.Net+XML操作基類(修改,刪除,新增,創(chuàng)建)
- asp.net下創(chuàng)建、查詢、修改帶名稱空間的 XML 文件的例子
- asp.net實(shí)現(xiàn)在XmlTextWriter中寫入一個(gè)CDATA的方法
- asp.net簡(jiǎn)單生成XML文件的方法
- asp.net實(shí)現(xiàn)輸出xml的方法
- ASP.NET XmlDocument類詳解
- ASP.NET實(shí)現(xiàn)TreeView的XML數(shù)據(jù)源綁定實(shí)例代碼
- ASP.NET中的Menu控件的應(yīng)用及XmlDataSource的了解
- ASP.NET+XML打造網(wǎng)絡(luò)硬盤原理分析
- ASP.NET讀取XML文件4種方法分析
- asp.net下XML的加密和解密實(shí)現(xiàn)方法
- asp.net創(chuàng)建XML文件的方法小結(jié)
相關(guān)文章
基于.NET BitmapImage 內(nèi)存釋放問題的解決方法詳解
本篇文章是對(duì).NET BitmapImage 內(nèi)存釋放問題的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05VS2010發(fā)布Web網(wǎng)站技術(shù)攻略
本篇文章主要包含了完整的發(fā)布網(wǎng)站步驟、發(fā)布網(wǎng)站過程中可能遇到的問題,以及配套的解決方法,相信感興趣的朋友一定會(huì)喜歡這篇文章的2015-07-07WPF集合控件實(shí)現(xiàn)分隔符(ItemsControl Separator)
這篇文章主要為大家詳細(xì)介紹了WPF集合控件實(shí)現(xiàn)分隔符ItemsControl Separator,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04.Net Core WebApi部署在Linux服務(wù)器上的方法
這篇文章主要介紹了.Net Core WebApi部署在Linux服務(wù)器上的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03asp.net LC.exe已退出代碼為 -1的原因分析及解決方法
錯(cuò)誤“LC.exe”已退出,代碼為 -1。是VS2005,并且在項(xiàng)目中引用了第三方組件。2013-06-06