国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET中根據XML動態創建使用WEB組件

2024-07-10 13:13:40
字體:
來源:轉載
供稿:網友

前段時間筆者在開發中需要動態創建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組件”部分會有較為詳細的介紹)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 治县。| 衡阳市| 安宁市| 淅川县| 永宁县| 合水县| 合江县| 依安县| 泾川县| 佳木斯市| 家居| 满洲里市| 轮台县| 自贡市| 万宁市| 湖北省| 贵定县| 德江县| 清镇市| 新乡县| 手游| 图片| 灵寿县| 中牟县| 辉县市| 潮州市| 尖扎县| 邯郸县| 宾川县| 黄大仙区| 任丘市| 财经| 宜黄县| 来凤县| 封开县| 海伦市| 秀山| 镇宁| 宜春市| 峨眉山市| 固始县|