1

Just curious!

I was wondering, what actually happens so that any control that is defined in .aspx page and having "runat" attribute is accessible in code behind file?

.aspx page:

code behind page: lbl.Text = "This is a label";

If anyone can share some information about what actually is happening here, what makes my label accessible in code behind after adding "runat" attribute?

sunil20000
  • 356
  • 3
  • 16

1 Answers1

2

Every time you change your aspx file Visual Studio will regenerate a file named {YourPageName}.aspx.designer.cs and declare controls with attribute runat="server" in it.

So if you have a label control in your aspx file like this:

then there is a variable declared in your .aspx.designer.cs file which is auto generated like this:

protected global::System.Web.UI.WebControls.Label lbl;

Since your page is declared as a partial class you can access lbl in code behind file.

You can open the file and take a look at its content.

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
  • I found from MSDN forums, "If you create website, the webform would contain only aspx file and aspx.cs file. If you create web application, the webform would contain aspx file, aspx.cs and aspx.designer.cs file.". In case of WebApplication, designer files helps us to access controls on code behind page but what about WebSite, where no designer file available. How controls are still accessible on code behind page? – sunil20000 Mar 19 '17 at 11:18