Saturday, March 31, 2012

Strange behavior by the codebehind

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

"dw" <cougarmana_NOSPAM@.uncw.edu> wrote in message
news:%23ajHX3ClEHA.3340@.TK2MSFTNGP14.phx.gbl...
> 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.
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

--
MY ASP.Net tutorials
http://www.openmymind.net/

"dw" <cougarmana_NOSPAM@.uncw.edu> wrote in message
news:%23ajHX3ClEHA.3340@.TK2MSFTNGP14.phx.gbl...
> 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.
On Mon, 6 Sep 2004 12:23:20 -0400, dw <cougarmana_NOSPAM@.uncw.edu> wrote:

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

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET
"dw" <cougarmana_NOSPAM@.uncw.edu> wrote in message
news:%23ajHX3ClEHA.3340@.TK2MSFTNGP14.phx.gbl...
> 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
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 :)

"WJ" <JohnWebbs@.HotMail.Com> wrote in message
news:uXLn7NDlEHA.3496@.TK2MSFTNGP12.phx.gbl...
> "dw" <cougarmana_NOSPAM@.uncw.edu> wrote in message
> news:%23ajHX3ClEHA.3340@.TK2MSFTNGP14.phx.gbl...
> > 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
On Mon, 6 Sep 2004 12:43:31 -0400, Karl <karl REMOVE @. REMOVE openmymind
REMOVEMETOO . ANDME net> wrote:

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

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET
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...

--
Scott
http://www.OdeToCode.com

On Mon, 06 Sep 2004 12:18:21 -0500, "Craig Deelsnyder"
<cdeelsny@.no_spam_4_meyahoo.com> wrote:

>On Mon, 6 Sep 2004 12:43:31 -0400, Karl <karl REMOVE @. REMOVE openmymind
>REMOVEMETOO . ANDME net> wrote:
>>
>> 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...

0 comments:

Post a Comment