(C#)动态生成控件成功,引用生成的控件失败(附程序)
我在程序中动态生成了一个表格,在表格里动态生成了一些label和textbox,生成没有问题。现在我想得到用户在这些label和textbox中填入的数据,使用FindControl不能得到这些控件。请高手指点。程序如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace datashow
{
/// <summary>
/// writeconfigfile 的摘要说明。
/// </summary>
public class writeconfigfile : System.Web.UI.Page
{
protected System.Web.UI.WebControls.RangeValidator totalnumRangeValidator;
protected System.Web.UI.HtmlControls.HtmlGenericControl firststep;
protected System.Web.UI.HtmlControls.HtmlGenericControl secondstep;
protected System.Web.UI.WebControls.TextBox totalnumTBox;
protected System.Web.UI.HtmlControls.HtmlSelect nettypeselect;
protected System.Web.UI.HtmlControls.HtmlGenericControl thirdstep;
protected System.Web.UI.HtmlControls.HtmlSelect CellsSelect;
protected System.Web.UI.WebControls.Button nextButton3;
protected System.Web.UI.WebControls.Button nextButton2;
protected System.Web.UI.WebControls.Button nextButton1;
protected System.Web.UI.WebControls.Button summitButton;
protected System.Web.UI.HtmlControls.HtmlGenericControl fourthDiv;
protected System.Web.UI.HtmlControls.HtmlGenericControl showresultDiv;
protected System.Web.UI.WebControls.PlaceHolder myPlaceHolder;
//自定义变量
Label sequencedataLabel;//测量点序号
TextBox nameTB;//测量点名称
TextBox ipTB;//测量点IP
//Button summitbtn;//提交按钮
//定义测量点的名称数组和ip数组,长度定为50,应该够用了
int[] sequenceint=new int [50];
string[] namestr = new string [50];
string[] ipstr = new string [50];
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
secondstep.Visible=false;
thirdstep.Visible=false;
fourthDiv.Visible=false;
if (!IsPostBack)
{
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.nextButton1.Click += new System.EventHandler(this.nextButton1_Click);
this.nextButton2.Click += new System.EventHandler(this.nextButton2_Click);
this.nextButton3.Click += new System.EventHandler(this.nextButton3_Click);
this.summitButton.Click += new System.EventHandler(this.summitButton_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void nextButton1_Click(object sender, System.EventArgs e)
{
//得到测量点的总数
ViewState["totalnum"]=totalnumTBox.Text;
firststep.Visible=false;
secondstep.Visible=true;
}
private void nextButton2_Click(object sender, System.EventArgs e)
{
firststep.Visible=false;
secondstep.Visible=false;
thirdstep.Visible=true;
//得到网络的拓朴特征(星形或网状)
ViewState["nettype"]=nettypeselect.Value;
}
//在这里生成表格
private void nextButton3_Click(object sender, System.EventArgs e)
{
firststep.Visible=false;
secondstep.Visible=false;
thirdstep.Visible=false;
fourthDiv.Visible=true;
//得到列数
ViewState["Cells"]=int.Parse(CellsSelect.Value);
//根据得到测量点的总数,动态生成输入框
for(int i=1;i<=int.Parse(ViewState["totalnum"].ToString());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=false;
tCell.Controls.Add(sequenceLabel);
tRow.Cells.Add(tCell);
//创建第二列
tCell = new HtmlTableCell();
sequencedataLabel = new Label();
sequencedataLabel.ID="sequencedataLabel"+i.ToString();
sequencedataLabel.Text=i.ToString();
sequencedataLabel.Enabled=false;
tCell.Controls.Add(sequencedataLabel);
tRow.Cells.Add(tCell);
//创建第三列
tCell = new HtmlTableCell();
Label nameLabel = new Label();
nameLabel.ID="nameLabel"+i.ToString();
nameLabel.Text="名称:";
nameLabel.Enabled=false;
tCell.Controls.Add(nameLabel);
tRow.Cells.Add(tCell);
//创建第四列
tCell = new HtmlTableCell();
nameTB=new TextBox();
nameTB.ID="nameTB"+i.ToString();
nameTB.Width=120;
nameTB.MaxLength=50;
tCell.Controls.Add(nameTB);
tRow.Cells.Add(tCell);
//创建第五列
tCell = new HtmlTableCell();
Label ipLabel = new Label();
ipLabel.ID="ipLabel"+i.ToString();
ipLabel.Text="IP:";
ipLabel.Enabled=false;
tCell.Controls.Add(ipLabel);
tRow.Cells.Add(tCell);
//创建第六列
tCell = new HtmlTableCell();
ipTB=new TextBox();
ipTB.ID="ipTB"+i.ToString();
ipTB.Width=120;
ipTB.MaxLength=15;
tCell.Controls.Add(ipTB);
tRow.Cells.Add(tCell);
}
}
//想在这里得到数据
private void summitButton_Click(object sender, System.EventArgs e)
{
firststep.Visible=false;
secondstep.Visible=false;
thirdstep.Visible=false;
fourthDiv.Visible=false;
showresultDiv.Visible=true;
showresultDiv.InnerHtml="总数:"+ViewState["totalnum"]+
"<br>网络拓朴:"+ViewState["nettype"]+
"<br列数:"+ViewState["Cells"]+
"<br>测量点:<br>";
for(int icount=1;icount<=int.Parse(ViewState["totalnum"].ToString());icount++)
{
string sequencedataLabelid="sequencedataLabel"+icount.ToString();
Label sequencedata=(Label)myPlaceHolder.FindControl(sequencedataLabelid);
//在这里报错!!!!!!!
string sequenceinttext=sequencedata.Text.ToString();
sequenceint[icount]=int.Parse(sequencedataLabel.Text);
nameTB=(TextBox)myPlaceHolder.FindControl("nameTB"+icount.ToString());
namestr[icount]=nameTB.Text;
ipTB=(TextBox)myPlaceHolder.FindControl("ipTB"+icount.ToString());
ipstr[icount] =ipTB.Text;
showresultDiv.InnerHtml+="序号:"+sequenceint[icount]+"名称:"+namestr[icount]+"IP:"+ipstr[icount]+"<br>";
}
}
}
}
报错信息为:
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
源错误:
行 204: "<br>测量点:<br>";
行 205: Control myshowtable=myPlaceHolder.FindControl("myshowtable");
行 206: string tablename=myshowtable.ID.ToString();
行 207: for(int icount=1;icount<=int.Parse(ViewState["totalnum"].ToString());icount++)
行 208: {
其中行 206:string tablename=myshowtable.ID.ToString();是报错点。
我跟踪程序,发现myshowtable在运行时的值是<未定义的值>,所以我考虑是不是当summitButton被点击后,动态创建的控件便失效了,所以不能引用。如果是这样,那莫应该如何处理程序以满足我的要求(即根据用户输入的数字动态生成label和textbox,并且要能得到用户在label和textbox输入的数据)?
请各位帮忙!!谢谢大家!