One quick question about .net's using control ID as an instancevariable

  • Thread starter Thread starter Author #1
  • Start date Start date
A

Author #1

I am just curious about this.

We declare a Label control in our web form as

<asp:Label ID="lblMessage" runat="server" Text="Hello"></asp:Label>

Of course, the ID "lblMessage" as given above is of type String.

But, in our code-behind, we can simply use lblMessage as the instance
variable of that declared Label control. What does the .net framework
do behind the scenes? Does it do something like below to link the
variable lblMessage to the control which has the ID "lblMessage"?

Label lblMessage = new Label();
lblMessage.ID = "lblMessage";
 
It most certainly does - ASP.NET just hides that from you in the designer
file. If you open it up, you'll see the following line:
protected global::System.Web.UI.WebControls.Label lblMessage;

However, in a WinForms project, the code that you deduced above is exactly
what you will see...

Thank you. Yes, I do see lines like below in my designer file. So, I
guess it is safe to say that the .net framework does some dirty work
behind the scenes to link the lblMessage instance to the declared
control whose ID is "lblMessage".

protected global::System.Web.UI.WebControls.Label lblMessage;
 
I am just curious about this.

We declare a Label control in our web form as

<asp:Label ID="lblMessage" runat="server" Text="Hello"></asp:Label>

Of course, the ID "lblMessage" as given above is of type String.

But, in our code-behind, we can simply use lblMessage as the instance
variable of that declared Label control. What does the .net framework
do behind the scenes? Does it do something like below to link the
variable lblMessage to the control which has the ID "lblMessage"?

Label lblMessage = new Label();
lblMessage.ID = "lblMessage";


You must be on ASP.NET 2.0 or greater, as 1.x had this in the code
behind file. In 2.0 or greater (which are really additions to 2.0, i.e.
3.0 and 3.5), you end up with a designer file that contains the
declarations of controls on the forms, as well as instantiation.

This is not completely true, as there are some differences when using
the web site template rather than the web application template, but
going through all of the difference is more of a novel than I am willing
to write right now. ;-)

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
You must be on ASP.NET 2.0 or greater, as 1.x had this in the code
behind file. In 2.0 or greater (which are really additions to 2.0, i.e.
3.0 and 3.5), you end up with a designer file that contains the
declarations of controls on the forms, as well as instantiation.

This is not completely true, as there are some differences when using
the web site template rather than the web application template, but
going through all of the difference is more of a novel than I am willing
to write right now. ;-)

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog:http://gregorybeamer.spaces.live.com

*******************************************
|      Think outside the box!             |
*******************************************

Yeah, I know the differences between a website project and a web
application project. Actually a while back, I did quite some research
online about them. I am using both for two of my projects.
 
Back
Top