Changing a control from your C# code
Once a control has an identifier, your C# code can reach it by name. That is how a page changes while it is running.
Read a value, set a value
C#
protected void sendButton_Click(object sender, EventArgs e)
{
string typed = nameBox.Text; // read
greetingLabel.Text = "Hello, " + typed; // set
greetingLabel.Visible = true;
}What to notice
- Reading a property gives you the value the visitor supplied.
- Setting a property changes what the browser will draw next.
- Properties such as visible let you show and hide whole parts of a page.
Guard against empty input. If the visitor sends nothing, the text is an empty string. Check for that before you use it. The required field lesson shows a cleaner way.
Good identifiers make this code readable, which is the subject of naming controls.