Can't access controls in code behind

D

David A. Osborn

I have a web project inVS2005 that I copied some existing pages from a
website that I previously created. To clarify the website was originally
created in Visual Web Developer Express and not I have created a web project
(not a website) in VS2005 and add a few of the pages to this web project
One of the pages I am currently working with is not allowing me to reference
the controls from the design page in the code behind. Do I need to make
some adjustments to the pages since they were originally in a website and
are now in a web project? Thanks for the help.
 
L

Laurent Bugnion

Hi,
I have a web project inVS2005 that I copied some existing pages from a
website that I previously created. To clarify the website was originally
created in Visual Web Developer Express and not I have created a web project
(not a website) in VS2005 and add a few of the pages to this web project
One of the pages I am currently working with is not allowing me to reference
the controls from the design page in the code behind. Do I need to make
some adjustments to the pages since they were originally in a website and
are now in a web project? Thanks for the help.

You must add runat="server" to all the controls that you wish to access
from the code behind, and of course they must have an ID.

Greetings,
Laurent
 
D

dosborn278

Both of these items are included. I'm guessing it has something to do
with the fact that the files were originally in a website solution and
now that they are added to a web project, they are some how not tied
into the project properly.
 
L

Laurent Bugnion

Hi,

Both of these items are included. I'm guessing it has something to do
with the fact that the files were originally in a website solution and
now that they are added to a web project, they are some how not tied
into the project properly.

In ASP.NET 2.0, the controls are added to the code-behind by using an
additional CS file named "ClassName.designer.cs", which contains a
partial class. In Website projects, this file is created automatically.
When you moved the files to the web application project, it might very
well be that the link has been broken.

You have two solutions: Add a new Web Form to your web app project and
copy the file (ASPX and CS) into the corresponding new files. This will
restore the link to the designer.cs file.

Another way is to simply declare the controls yourself in the code behind:

// For example
protected Button bnClickMe;

It is sufficient to declare them, they will be automatically
instantiated by the framework. Note that they must be declared protected .

HTH,
Laurent
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top