当前位置:编程学习 > C#/ASP.NET >>

为什么这样动态创建组件后,获取不了值

页面代码

                                                        <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中找 --------------------编程问答--------------------
引用 1 楼 cpp2017 的回复:
去掉 if (!this.IsPostBack) 
--------------------编程问答-------------------- if (!this.IsPostBack) 
=======================
不可以 --------------------编程问答-------------------- 找的路径不对呀,怎么放就怎么取 --------------------编程问答-------------------- (TabCond.FindControl("TbQuery1") as TextBox).Text
==================================================
也取不到啊
--------------------编程问答-------------------- string str=(TabCond.FindControl("TbQuery1") as TextBox).Text; 
--------------------编程问答--------------------
引用 3 楼 isline 的回复:
引用 1 楼 cpp2017 的回复:
去掉 if (!this.IsPostBack)

最简单的 方法;至于原理 建议楼主多看看 asp.net页面生命周期
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,