Find control on form and set value using C#

A simple example how to use FindControl to search for controls and objects on the current form with asp.net c# and set the properties and values.

Used
Microsoft Visual studio 8 with .net 3.5

The Form

Create a form with:

1 x TextBox name the TextBox id "TextBox1"
1 x Button

The Code

In the button click procedure place the following code.

            TextBox control = (TextBox)this.FindControl("TextBox1");
            if (control != null)
            {
                control.Text = "found control!";
                control.BackColor = System.Drawing.Color.DarkRed;
            }

When the click procedure has found the TextBox the Text value will be changed and the BackColor will be set.

Taxonomy