ASP.NET

Find Control by ID

Syntax#

  1. control.FindControl("Id Of The Control To Be Found")

Remarks#

  • FindControl is not recursive, it only searches through immediate children of the control
  • There is an overload FindControl(String, int) which is not indented for public usage
  • If nothing is found, FindControl returns null, so this is often a good idea to verify result for being not null

Accessing the TextBox Control in aspx Page

TextBox txt = (TextBox)FindControl(yourtxt_Id);

Find a control in a GridView, Repeater, ListView etc.

If the Control has rows.

TextBox tb = GridView1.Rows[i].FindControl("TextBox1") as TextBox;

Or if it has items.

TextBox tb = Repeater1.Items[i].FindControl("TextBox1") as TextBox;

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow