简体   繁体   English

ASP.NET FindControl的文本框和获取价值

[英]ASP.NET FindControl Of Textbox and Get Value

I added a textbox dynamically as follow: 我动态添加了一个文本框,如下所示:

        TextBox tb = new TextBox();

        tb.ID = "Tb1;
        tb.Text = "Tb1";




        Panel1.Controls.Add(tb);

When I try to retrieve the value, it fails because apparently it can't find it and it's assigned null : 当我尝试检索该值时,它失败,因为显然无法找到它,并且分配了null:

                TextBox tb = Page.FindControl("Tb1") as TextBox;
                Label1.Text = tb.Text; //this doesn't work because tb is apparently null 
TextBox tb = Panel1.FindControl("Tb1") as TextBox;

The FindControl method can be used to access a control whose ID is not available at design time. FindControl方法可用于访问其ID在设计时不可用的控件。 The method searches only the page's immediate, or top-level, container; 该方法仅搜索页面的直接或顶层容器; it does not recursively search for controls in naming containers contained on the page. 它不会在页面上包含的命名容器中递归搜索控件。 To access controls in a subordinate naming container, call the FindControl method of that container. 若要访问从属命名容器中的控件,请调用该容器的FindControl方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM