为什么这样动态创建组件后,获取不了值
页面代码
<tr>
<td valign="top">
<asp:Table ID="TabCond" runat="server" BorderWidth="0" CellPadding="0" CellSpacing="1" BackColor="#d4e1e9" Width="100%">
</asp:Table>
</td>
</tr>
创建代码
if (!this.IsPostBack)
{
TableRow trRow = new TableRow();
TableCell tcCellTitle = new TableCell();
tcCellTitle.HorizontalAlign = HorizontalAlign.Right;
tcCellTitle.Style.Value= "background-color: #e7ebef; height: 30px; width: 100px";
tcCellTitle.Text = " <div style='text-align: center'>测试标题</div>";
trRow.Cells.Add(tcCellTitle);
TableCell tcCellContent = new TableCell();
tcCellContent.HorizontalAlign = HorizontalAlign.Left;
tcCellContent.Style.Value = "background-color: White";
tcCellContent.Text = " ";
TextBox TbQuery = new TextBox();
TbQuery.ID = "TbQuery1";
TbQuery.BorderStyle = BorderStyle.Groove;
tcCellContent.Controls.Add(TbQuery);
trRow.Cells.Add(tcCellContent);
TabCond.Rows.Add(trRow);
}
调用代码
(this.FindControl("TbQuery1") as TextBox).Text
提示对象不存在,那我怎么动态创建,textbox可以获取值。
--------------------编程问答-------------------- 去掉 if (!this.IsPostBack)
--------------------编程问答-------------------- 试试:在TabCond中找 --------------------编程问答-------------------- --------------------编程问答-------------------- if (!this.IsPostBack)
=======================
不可以 --------------------编程问答-------------------- 找的路径不对呀,怎么放就怎么取 --------------------编程问答-------------------- (TabCond.FindControl("TbQuery1") as TextBox).Text
==================================================
也取不到啊
--------------------编程问答-------------------- string str=(TabCond.FindControl("TbQuery1") as TextBox).Text;
--------------------编程问答--------------------
最简单的 方法;至于原理 建议楼主多看看 asp.net页面生命周期
补充:.NET技术 , ASP.NET