How server side controls are accessible in code behind files in Asp.Net?

927 views Asked by At

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?

1

There are 1 answers

1
Hamid Pourjam On BEST ANSWER

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.