Strange behavior by the codebehind

D

dw

Hello, all. I created a few Label objects, and deleted some. However, later
when I was using the codebehind page in VS .NET 2003 to set the Text of the
existing labels, I noticed that when I hit the period at the end of this"
"Me." I got the labels that had been deleted from the form's HTML. I
accidentally selected an item that was no longer on the form, and it took me
a few hours to debug that error "Object reference not set to an instance of
an object." I ultimately had to shut VS .Net down and restart it before I
even saw the new labels I had added. Hitting Refresh at the top of the
Solution Explorer didn't help.

Is this a known issue? Why am I still seeing the deleted Label objects when
I hit "Me."? Anyway to clear those items out permanently? I usually test my
pages without doing Rebuild, but by hitting F5. Could that have something to
do with it? Thanks.
 
G

Greg Burns

I think I've seen what you are talking about. Any time I add control in
HTML, I ALWAYS swith to the Design tab. This seems to synchronize the
codebehind (it doesn't always remove old controls though). Of course, some
people avoid the Design tab like the plague, in which case you need to add
your code-behind declarations manually.

In your codebehind, if you expand the #Region " Web Form Designer Generated
Code " section. You'll see all the current (and probably past) control
declarations you had on your form. It is safe to delete any orphaned ones.
Greg
 
C

Craig Deelsnyder

Hello, all. I created a few Label objects, and deleted some. However,
later
when I was using the codebehind page in VS .NET 2003 to set the Text of
the
existing labels, I noticed that when I hit the period at the end of this"
"Me." I got the labels that had been deleted from the form's HTML. I
accidentally selected an item that was no longer on the form, and it
took me
a few hours to debug that error "Object reference not set to an instance
of
an object." I ultimately had to shut VS .Net down and restart it before I
even saw the new labels I had added. Hitting Refresh at the top of the
Solution Explorer didn't help.

Is this a known issue? Why am I still seeing the deleted Label objects
when
I hit "Me."? Anyway to clear those items out permanently? I usually test
my
pages without doing Rebuild, but by hitting F5. Could that have
something to
do with it? Thanks.

VS.NET does not delete those old references very well (the variable
declarations at the top of the code-behind class, sometimes they're hidden
in the auto-generated section, that's why you see them). I know for a
fact if you're going to rename a control on the aspx, do it in the Design
View, in the Properties window for that control. Otherwise the name does
not get updated on the codebehind (especially if you do it in the HTML
view).

As far as deleting, I'm not sure, but it's pry just as quirky. Try
deleting it on the Design view, not in the HTML view of the aspx. Then I
usually always switch to Design view after updating my .aspx and
double-click on the document to have VS.NET switch over to the code-behind
automatically. Seems it's then able to do things a little more cleanly,
though things you delete may still not get cleaned up as well.
 
K

Karl

I wouldn't call it a bug, just a limitation of what VS.Net can safely do.
In your codebehind, you'll notice a region named #Region " Web Form Designer
Generated Code ", if you expand this, you'll see all those labels you
deleted in the aspx file still declared. That's what Me. uses to show that
list...you can safely delete them.

When VS.Net adds those controls to that region, it does so 'cuz it found
them on your page, but when you remove them, vs.net can't safely remove
them..just because it isn't on the page doesn't mean you aren't dynamically
creating them in codebehind.

The new code-besides model of ASP.Net 2.0 makes these declaration dissapear,
along with the problem.

Karl
 
W

WJ

dw said:
I got the labels that had been deleted from the form's HTML.

1. Make sure that you always mess around with the IDE tab, not the HTML tab.

2. Also, leave the auto generated code Region alone, do not add/modify this
region because IDE thinks that it no longer owns it and will not clean it
properly for you. I tried to trick it by adding couple of blanks and I can
repeat the problem.

3. Keep in mind that it will never get grid of "user's event/behavior" for
you, which is good, you will have to clean it yourself if you decide not to
keep the code.

John
 
D

dw

Thank you, Greg, Craig, Karl, and WJ. All make perfect sense. Yes, I was
editing the code in HTML, and so VS couldn't know of the items I'd added or
deleted. For awhile there, I thought I'd broken VS. Thanks for your help :)
 
C

Craig Deelsnyder

When VS.Net adds those controls to that region, it does so 'cuz it found
them on your page, but when you remove them, vs.net can't safely remove
them..just because it isn't on the page doesn't mean you aren't
dynamically
creating them in codebehind.
I always wondered if that was what they were really doing, but I still
considered it annoying....

Unfortunately being an OO purist, I'm not too high on the partial type
approach of 2.0 either, oh well :) I still have my business layer...
 
S

Scott Allen

Particularly annoying is that I cannot set the base class for my page
in code, I have to modify the Inherits attribute VS.NET sticks in the
@ Page directive....
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top