How to convert 1.1 code-behind page to 2.0 format?

A

Alan Silver

Hello,

I have installed the 2.0 framework, and am looking at converting some of
my 1.1 pages to use partial classes. I don't (yet) have VS2005, so I'm
doing this by hand, but am having problems.

I have a simple page that I made in the beta2 version of VWD. The code
behind looks like...

using System;
using System.Web.UI;

public partial class _Default : Page {
protected void Page_Load(object sender, EventArgs e) {
// do stuff here
}
}

From this, I assumed that all I needed to do to my existing code-behind
files was to change the class definition to be partial, and remove the
large list of lines like...

protected Literal litTest;
// etc

from the class definition. I did this, but the compiler complained that
litTest did not exist in the current context. If I put back the above
line, it worked.

The code-behind file that VWD generated didn't have the lines in
declaring each control in the code-behind, and it worked fine. What do I
need to do to get my file to work like that? I looked in the folder
where the files live, and couldn't see any other file relating to this
page. I seem to remember that 2.0 uses a third file, which contains
extra info. Is that what I'm missing? I couldn't see one for the page
VWD created, and I could move this page to another site and it worked
fine.

Thanks for any help.
 
A

Alan Silver

Anyone have an answer for this? Surely it must be a very common
question. Am I the only person trying to convert 1.1 pages to work with
2.0?
 
J

Juan T. Llibre

Try :

using System;
using System.Web.UI;
// etc...

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// do stuff here
}

}

re:
protected Literal litTest;
// etc

That *is* needed...

See :
http://msdn.microsoft.com/asp.net/beta2/beta2update.aspx
and
http://msdn.microsoft.com/asp.net/r...en-us/dnaspp/html/conversionissuesasp_net.asp
and
http://msdn.microsoft.com/asp.net/reference/migration/upgrade/default.aspx

for other critical changes going from 1.1 to 2.0
 
A

Alan Silver

Juan,

Thanks for the reply, but I'm still not clear!!

As far as the first bit of your post...
Try :

using System;
using System.Web.UI;
// etc...

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// do stuff here
}

}

I'm not sure what you're gaining by adding System.Web.UI.Page instead of
just Page, as the System.Web.UI namespace is already referenced at the
top of the file. Why would this make any difference?

More to the point however...
re:
protected Literal litTest;
// etc

That *is* needed...

Well, if you reread my original post, you will see that it seems it
isn't!! VWD created an .aspx and a code-behind for me, and the
"protected Literal litTest;" lines were *not* included. I copied these
two files to another web server entirely, one that has never seen VWD,
and the page ran fine.

Also...
for other critical changes going from 1.1 to 2.0

I've seen most of that before, but thanks for pointing it out anyway.

Whilst looking through those links, I came across
http://msdn.microsoft.com/asp.net/reference/migration/upgrade/default.asp
x?pull=/library/en-us/dnvs05/html/migratefromaspnetto2.asp, which shows
(just below figure 3) a new code-behind file, that does *not* have the
"protected..." lines in it.

Moreover, the text below the sample code explicitly states...

"The code-behind file has automatic access to any controls added to the
ASP.NET page"

.... which I read to support the fact that the code-behind file shown
doesn't included the references to the controls.

Hopefully you see why I didn't understand your reply. The code-behind
file that VWD generated didn't include the references, the MSDN article
seems to say that that they are not needed, but when I tried it on an
existing 1.1 file of mine, the compiler complained when I removed the
references. You say that they *are* needed. I'm confused ;-)

I would appreciate any explanation you have.

Thanks for the reply.
 
J

Juan T. Llibre

re:
I would appreciate any explanation you have.

I short-circuited ?

It happens every so often ( even to the best among us... )

;-)




Alan Silver said:
Juan,

Thanks for the reply, but I'm still not clear!!

As far as the first bit of your post...
Try :

using System;
using System.Web.UI;
// etc...

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// do stuff here
}

}

I'm not sure what you're gaining by adding System.Web.UI.Page instead of just Page, as
the System.Web.UI namespace is already referenced at the top of the file. Why would this
make any difference?

More to the point however...
re:
protected Literal litTest;
// etc

That *is* needed...

Well, if you reread my original post, you will see that it seems it isn't!! VWD created
an .aspx and a code-behind for me, and the "protected Literal litTest;" lines were *not*
included. I copied these two files to another web server entirely, one that has never
seen VWD, and the page ran fine.

Also...
for other critical changes going from 1.1 to 2.0

I've seen most of that before, but thanks for pointing it out anyway.

Whilst looking through those links, I came across
http://msdn.microsoft.com/asp.net/reference/migration/upgrade/default.asp
x?pull=/library/en-us/dnvs05/html/migratefromaspnetto2.asp, which shows (just below
figure 3) a new code-behind file, that does *not* have the "protected..." lines in it.

Moreover, the text below the sample code explicitly states...

"The code-behind file has automatic access to any controls added to the ASP.NET page"

... which I read to support the fact that the code-behind file shown doesn't included
the references to the controls.

Hopefully you see why I didn't understand your reply. The code-behind file that VWD
generated didn't include the references, the MSDN article seems to say that that they
are not needed, but when I tried it on an existing 1.1 file of mine, the compiler
complained when I removed the references. You say that they *are* needed. I'm confused
;-)

I would appreciate any explanation you have.

Thanks for the reply.
 
A

Alan Silver

re:
I short-circuited ?

It happens every so often ( even to the best among us... )

;-)

Glad to know it's not just me!!

OK, so if we agree that the control references aren't needed, are you
able to explain why I couldn't do this to an 1.1 existing page?

I changed the class declaration to be partial, and removed the list of
control declarations. That was all I could see that differentiated
between the 1.1 file I had and the 2.0 file VWD created. Oh, I also
changed the page directive of the .aspx file. When I tried to load it, I
got an exception saying a control (the first one referenced in the code)
didn't exist.

Any ideas? Thanks again
 
T

tdavisjr

The protected control references are not needed in the codebehind page
as the aspx file and the codebehind file sine asp.net combines these 2
classes into 1 class. This is what partial class brings to the table in
..NET 2.0.

In the @page directive there will be an attribute called Inherits which
sholud point to your _Default class and another attribute called
CodeFile which VWD usues to link both files in the designer.

As far as your error goes. It would be good to see what your aspx file
looks like since your codebehind file seems to be ok.
 
A

Alan Silver

<Plays theme from Twilight Zone>

Well, I don't know what I did differently now from the other day, but I
just tried it again and it worked fine!!

I wonder if I didn't change the Src attribute of the page directive to
CodeFile? Rereading my original post, I didn't mention doing that, so it
could be I missed that change.

Anyway, all seems to be working now. Thanks very much for your reply.
 

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

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top