简体   繁体   中英

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 :

                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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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