(C#)动态生成控件成功,引用生成的控件失败(附程序)

majormarshal 2004-05-13 09:30:21
我在程序中动态生成了一个表格,在表格里动态生成了一些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输入的数据)?

请各位帮忙!!谢谢大家!


...全文
197 18 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
softye 2004-07-21
  • 打赏
  • 举报
回复
UP
majormarshal 2004-05-13
  • 打赏
  • 举报
回复
问题已经解决,方法如下:
for(int icount=1;icount<=int.Parse(ViewState["totalnum"].ToString());icount++)
{

string sequenceinttext=Request.Form["sequencedataTB"+icount.ToString()].ToString();
sequenceint[icount]=int.Parse(sequenceinttext);

string nameTBtext=Request.Form["nameTB"+icount.ToString()].ToString();
namestr[icount]=nameTBtext;

string ipTBtext=Request.Form["ipTB"+icount.ToString()].ToString();
ipstr[icount] =ipTBtext;

showresultDiv.InnerHtml+="序号:"+sequenceint[icount]+" 名称:"+namestr[icount]+" IP:"+ipstr[icount]+"<br>";

}

其中的Label sequencedataLabel;//测量点序号
改为TextBox sequencedataTB;//测量点序号
因为Label 不能用Request.Form[]方法来引用,引用的话就会返回null。
majormarshal 2004-05-13
  • 打赏
  • 举报
回复
请大家帮忙啊。。不能提供解决思路的朋友也请帮忙顶啊。。。感谢了。。
majormarshal 2004-05-13
  • 打赏
  • 举报
回复
我的程序的思路是这样的:
1、用户输入要创建的label的个数。点击按钮,记下此数保存在ViewState["totalnum"]中
2、根据用户的输入动态创建label。
3、用户在动态创建label中填入数值。点击“提交”按钮
4、在“提交”按钮的click事件中获取动态创建的label.text。
问题在于,当用户点击“提交”按钮后,动态创建的label就失效了。那莫我该如何获得用户输入的值哪????

hbzxf 2004-05-13
  • 打赏
  • 举报
回复
学习
majormarshal 2004-05-13
  • 打赏
  • 举报
回复
to saucer(思归/MVP)
i have read the article u recommended to me..but it can't solve my problem..because i must create the table according the value of ViewState["totalnum"]...so..how can i deal with it..?
majormarshal 2004-05-13
  • 打赏
  • 举报
回复
使用Request.Form[sequencedataLabelid];返回的是null,sequencedataLabelid是我动态创建的label的ID。
cuike519 2004-05-13
  • 打赏
  • 举报
回复
saucer(思归/MVP) 出手果然不一样!
:-)
daou101 2004-05-13
  • 打赏
  • 举报
回复
好技术,http://blog.joycode.com/saucer/posts/19456.aspx
关注啊!
yjhyn 2004-05-13
  • 打赏
  • 举报
回复
Request.Form["Text/lable"];
用这个应该可以取得页面中的Text/lable
NekChan 2004-05-13
  • 打赏
  • 举报
回复
up
jackie615 2004-05-13
  • 打赏
  • 举报
回复
Request.Form["TextBox1"]
saucer 2004-05-13
  • 打赏
  • 举报
回复
you have to recreate those controls upon postback, see

http://support.microsoft.com/default.aspx?scid=kb;EN-US;317515

or

http://blog.joycode.com/saucer/posts/19456.aspx
combread 2004-05-13
  • 打赏
  • 举报
回复
Request.Form["TextBox1"]
combread 2004-05-13
  • 打赏
  • 举报
回复
Request["TextBox1"]是一个Object类型的变量,相当于(object)TextBox1.Text
majormarshal 2004-05-13
  • 打赏
  • 举报
回复
问题仍未解决。。。请高手帮忙啊!!!
majormarshal 2004-05-13
  • 打赏
  • 举报
回复
Request["TextBox1"] 返回的是string,这个string是个什莫哪?控件id?还是。。??请指点!
acewang 2004-05-13
  • 打赏
  • 举报
回复
因为是动态生成的,所以回发以后状态不能保存,如果你需要访问值你需要保证动态生成代码在每次Page_Load的时候执行,或者你这么访问:Request["TextBox1"] :-)

62,242

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧