Skip to main content
CSharpUniversity ASP.NET and C# lessons

The search runs entirely in your browser over the published lesson list.

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.

New lessons by email

Leave your address and get a short note whenever a fresh lesson is published. No account needed and you can stop at any time.

Thank you. Your address has been noted in this browser session. Connect a mailing service to deliver the notes for real.