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

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";
 
A

Author #1

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;
 
G

Gregory A. Beamer

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. ;-)
 
A

Author #1

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. ;-)

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top