The code-behind
is running; it's just not doing what you think it's doing.
Quote:
protected void Page_Load(object sender, EventArgs e)
{
Label Label1 = new Label();
Label1.Text = "This is a label!";
}
That creates a brand new
Label
instance; sets its text to
This is a label!
; then
throws it away, and never looks at it again.
Meanwhile, the
Label
defined in markup is never mentioned.
Change your code so that it uses the
Label1
field, rather than creating a local variable with the same name.
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "This is a label!";
}
NB: WebForms is an ancient and long-dead technology. It sounds like you're starting a new project, which means you should be using a more modern framework - probably
ASP.NET Core[
^], using MVC, Razor Pages, or Blazor.