在动态添加的一个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>
你确定你的控件在Cells[1]吗?
if(IsPostBack)
-->
if(!IsPostBack)
改成这样就可以了
<%@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>