ASP.NET创建并使用Web组件(2)

<script language='javascript' src='http://www.shiqiaotou.com/donetk/Header.js'></script>    三、动态创建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组件”部分会有较为详细的介绍)<script language='javascript' src='http://www.shiqiaotou.com/donetk/Footer.js'></script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值