Click here to Skip to main content
16,018,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
<asp:Label rundat = "server" ID= "lbl" />

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?

What I have tried:

I've tried to find details but getting only how to access controls in code behind page not the actual machinism behind this.
Posted
Updated 20-Mar-17 0:25am

1 solution

Quote:
what makes my label accessible in code behind after adding "runat" attribute?


Whenever you do some change in the ASPX page, the designer file will get updated everytime when the runat="server" attribute is added to the control.
Since the designer file is a partial class, the protected members will be accessed in the code behind file.

for example
when you add an asp button to the page
<asp:Button ID="btn" runat="server" />

in the designer file, it will update as
protected global::System.Web.UI.WebControls.Button btn;

and hence the btn object is accessible in the code behind, since code behind and designer file are acting as partial class.

refer:
c# partial class[^]
Partial Classes and Methods (C# Programming Guide)[^]
 
Share this answer
 
v2
Comments
sunil kumar meena 20-Mar-17 13:58pm    
Thanks for reply, but I never saw Designer file in Asp.net. I've searched entire project but couldn't find such file. So, where can I find this file in my solution?

Another thing is that, 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?
Karthik_Mahalingam 20-Mar-17 21:49pm    
refer this
http://stackoverflow.com/a/22249090

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900