关于动态添加的控件取值问题!

fengyuyan 2003-08-06 11:23:30
在动态添加的一个Table中的一个TableCell中添加一个TextBox加到From后如何在提交后获得TextBox中的值?
代码如下:
<%@Page language="C#" debug=true %>
<%@import NameSpace="System.IO" %>
<script runat=server>

void Page_Load(object sender,System.EventArgs e){

if(IsPostBack){
TextBox txt1= (TextBox)t.Rows[0].Cells[1].FindControl("foldername");//找不到TextBox的值
Response.Write((string)txt.Text);
}

Table t =new Table();
TableRow tr =new TableRow();
TableCell tc_txt=new TableCell();
TableCell tc_controls=new TableCell();
TableCell tc_button=new TableCell();

tc_txt.Text="创建文件夹";
TextBox txt=new TextBox();
txt.ID="foldername";
tc_controls.Controls.Add(txt);

Button bt=new Button();
bt.Text="提交";
tc_button.Controls.Add(bt);

tr.Cells.Add(tc_txt);
tr.Cells.Add(tc_controls);
tr.Cells.Add(tc_button);

t.Rows.Add(tr);

tc_txt.Dispose();
tc_controls.Dispose();
tc_button.Dispose();
tr.Dispose();
txt.Dispose();
form1.Controls.Add(t);

}
</SCRIPT>
<html>
<head>
<title></title>
</head>
<body>
<form id=form1 runat=server>

</form>
</body>
</html>
...全文
53 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
panyee 2003-08-07
  • 打赏
  • 举报
回复
动态加上按钮事件, 但事件函数事先写好
bt.Click += new System.EventHandler(this.Button1_Click);

在Button1_Click里执行一些事件
panyee 2003-08-07
  • 打赏
  • 举报
回复
改成这样就可以了

<%@Page language="C#" debug=true %>
<%@import NameSpace="System.IO" %>
<script runat=server>
Table t = null;
void Page_Load(object sender,System.EventArgs e){



t =new Table();


TableRow tr =new TableRow();
TableCell tc_txt=new TableCell();
TableCell tc_controls=new TableCell();
TableCell tc_button=new TableCell();

tc_txt.Text="创建文件夹";
TextBox txt=new TextBox();
txt.ID="foldername";
tc_controls.Controls.Add(txt);

Button bt=new Button();
bt.Text="提交";
bt.Click += new System.EventHandler(this.Button1_Click);
tc_button.Controls.Add(bt);

tr.Cells.Add(tc_txt);
tr.Cells.Add(tc_controls);
tr.Cells.Add(tc_button);

t.Rows.Add(tr);

tc_txt.Dispose();
tc_controls.Dispose();
tc_button.Dispose();
tr.Dispose();
txt.Dispose();
form1.Controls.Add(t);

}
void Button1_Click(Object sender, EventArgs e)
{
TextBox txt1= (TextBox)t.Rows[0].Cells[1].FindControl("foldername");
Response.Write((string)txt1.Text);

}
</SCRIPT>
<html>
<head>
<title></title>
</head>
<body>
<form id=form1 runat=server>
<div id=div1 runat=server></div>
</form>
</body>
</html>
fengyuyan 2003-08-06
  • 打赏
  • 举报
回复
都试过了没用。
这段就是动态创建表格的代码。我要的是那个TextBox的值。
Table t =new Table();
TableRow tr =new TableRow();
TableCell tc_txt=new TableCell();
TableCell tc_controls=new TableCell();
TableCell tc_button=new TableCell();

tc_txt.Text="创建文件夹";
TextBox txt=new TextBox();
txt.ID="foldername";
tc_controls.Controls.Add(txt);

Button bt=new Button();
bt.Text="提交";
tc_button.Controls.Add(bt);

tr.Cells.Add(tc_txt);
tr.Cells.Add(tc_controls);
tr.Cells.Add(tc_button);

t.Rows.Add(tr);

tc_txt.Dispose();
tc_controls.Dispose();
tc_button.Dispose();
tr.Dispose();
txt.Dispose();
form1.Controls.Add(t);
希偌 2003-08-06
  • 打赏
  • 举报
回复
你确定你的控件在Cells[1]吗?
if(IsPostBack)
-->
if(!IsPostBack)

62,074

社区成员

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

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

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

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