简体   繁体   中英

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM