Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8
This is the forum to discuss the Wrox book Beginning ASP.NET 2.0 by Chris Hart, John Kauffman, David Sussman, Chris Ullman; ISBN: 9780764588501
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 11th, 2006, 07:23 PM
Friend of Wrox
 
Join Date: Apr 2005
Location: Fort Walton Beach, FL, USA.
Posts: 190
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chap 10 Ex. 1 Solution, Appendix A, p. 638

This is not a very helpful ‘Solution’ for me because:

1. The very first thing it says is: “Create a class called CodeBehind.cs. Where? Should one put the new class inside a App_Code folder? or not? There is not an App_Code folder anyway. Should I make one? Should the new class be directly under the uppermost project folder? Trying to put it directly under the project brings forth a message, ‘Do you really want to do that? I don’t know. Author should have told me where to put it.

2. Does author really want me to make a C# named folder (.cs) when everything to date has been VB?
The Page directive specifies language=VB.

3. I’ve noticed that so far in the book, all the code behind files start with the name of the webpage to which they apply. Shouldn’t this code behind file name include the .aspx?

4. OK. So I’ve made a VB Class folder and put it directly under the project. Don’t I want this code behind file’s icon to be shown indented and under the CodeBehind.aspx file?

5. When I try to drag the CodeBehind.vb file onto the CodeBehind.aspx file, I get a message that it won’t let me do that.

6. So, I proceed anyway leaving things where they are. When I double click on the CodeBehind.aspx to cause (hopefully) an empty Page_Load event handler to appear, it does not. So my hope that somehow the process would all turn out OK didn’t.

7. I have been trying since Chapter 1, on my own to find a way to make a codebehind.vb file for a page that I accidentally created without specifying to make one, and have yet to be able to do so. I thought this little exercise would show me how. Doesn’t. I don’t know, maybe this is one of those things that C# can do and VB cannot. I sure hope not!

8. Can anyone give me a clue how to get this mess straightened out?

VV


  #2 (permalink)  
Old February 13th, 2006, 06:34 AM
Wrox Author
 
Join Date: Jun 2004
Location: Liskeard, Cornwall, United Kingdom.
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
    Oops - apologies - seems to be a lot of errors that have crept through in this solution.

You're quite right it should be .vb. Also any new file that is created should be created in the same folder as the .aspx file. Up until this point in the book the aim is that you don't worry about code behind too much, it's mainly controls (except in chapters 6 and 9).

What happens is that when the code behind file is automatically created, it also amends the @Page directive for you in the .aspx file. It will add two attributes CodeFile and Inherits (as discussed on page 357). However a little bit more is done behind the scenes for you. Also the Code Behind is linked back to the page. An Inherits line is added to the Code Behind (.vb file). So you need to perform all of these steps manually.

This isn't specifically mentioned in the chapter, but you will see it if you examine the code behind files. Before you cry foul - this is meant to be a tough exercise (hence there are only two, and I wanted people to play around and try and get it working. In retrospect I think I should have added a hint - apologies again.) and if you look at the code you have already created in the Try It Outs and copy it, you will be on the right road.

The solution in the book seems to have a lot missing, I'll check my original files to see that something didn't get deleted by mistake, although I would have hoped an editor might have picked up on my omission!!

However here's a new solution, which I'll also post in errata. To get it to work:

1) Create a class called CodeBehind.vb in the same folder as CodeBehind.aspx. In the code behind, this code is automatically created:

Public Class CodeBehind

End Class

2) Add the line Inherits System.Web.UI.Page to it so that it reads:

Public Class CodeBehind Inherits System.Web.UI.Page

End Class

Strictly speaking you should also change the Public Class to Partial Class, but this change ISN'T necessary to get the solution working.

3) Go back to CodeBehind.aspx and add the following code to the @Page directive:

<%@Page CodeFile="CodeBehind.Vb" Inherits="CodeBehind" %>

3) Add the Label control to the page.

4) Go back to the Code Behind file and from the drop down boxes above the code select CodeBehind (the class name from the left) and select Load from the right. As mentioned on page 357 code is run in response to Page_Load event, so you will need to place your new label.txt code there. The code Label1.Text="Hello World" should be placed in the Page_Load event.

5) Run it.

Chris





Chris Ullman
Programmer/Technical Author
http://www.cuasp.co.uk
  #3 (permalink)  
Old February 13th, 2006, 01:04 PM
Friend of Wrox
 
Join Date: Apr 2005
Location: Fort Walton Beach, FL, USA.
Posts: 190
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Chris:
Again, much thanks for the help.
I have implemented your revised solution, as above, and with some fiddling got it to work.

I had to make one mod to your suggestion:

1. Your "4) Go back to the... " I found that I had to select on the left, PageEvents not CodeBehind. Then of course, the Page_Load event handler on the right is available.

2. I added the attribute of the previous Page directive, which only stated Language=VB, to the Page directive you suggested. That worked too. As an experiment,I removed the Language=VB attribute and found that caused no harm. (I guess then, that VB is the default language.)

3. I would like to make the Solution Explorer show the code behind file for every aspx page indented and directly underneath the aspx page for which it is intended but am not able to do that. I tried fiddling with the 'Nest Related Files' button on the menu bar of the Solution Explorer, but it does not seem to work. Tried dragging the partial file, renamed partial file from Private to Partial, and various other wild shots, but nothng will indent it and make it look pretty (properly nested underneath). Any suggestions on how I might do that.

Reason I would like to do that is because I am designing a 60 page site where many of the pages have similar sounding names. Am hoping to prevent confusion when working with the page's partial classes code which will have similarly spelled names. Thanks.

VV


  #4 (permalink)  
Old February 14th, 2006, 09:41 AM
Wrox Author
 
Join Date: Jun 2004
Location: Liskeard, Cornwall, United Kingdom.
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
    Re)
1) Hmm yes my typing is quite fast and ploughs ahead of my thinking sometimes - that's correct.

2) VB is the default language, yes, if you'd have been using C# I think it would have complained. However if you'd set up the language in VWD as C# for the site, then all the pages would by default be using C#.

3) On this one I can't help sorry. There must be something else VWD is doing behind the scenes to connect the code behind file to the aspx file in Solution Explorer. I've had a look around but can't see anything obvious. Maybe someone else knows... Dave or Imar??

Chris

Chris Ullman
Programmer/Technical Author
http://www.cuasp.co.uk
  #5 (permalink)  
Old February 14th, 2006, 10:40 AM
Imar's Avatar
Wrox Author
Points: 72,035, Level: 100
Points: 72,035, Level: 100 Points: 72,035, Level: 100 Points: 72,035, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,079
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi VictorVictor,

Here are the steps to change a normal page into a Code Behind file.

1. Create a blank web site

2. Add a normal page (make sure that "Place code in separate file" is unchecked). Name the page Test.aspx

3. Right-click the project, choose Add New Item and choose Class.
Name the file Test.aspx.vb. It's important that you name the file .aspx.vb, not just vb. It's this double extension that VWD uses to hook up nested files.
Click No when asked to add the file in the App_Code folder

4. At this stage, VWD should display the .vb file as a nested file in the Solution Explorer.

5. Change the <%@ Page directive from

<%@ Page Language="VB" %>

to

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Test.aspx.vb" Inherits="Test" %>

6. Modify the class file from:
Code:
Imports Microsoft.VisualBasic

Partial Class Test

End Class
to
Code:
Partial Class Test
    Inherits System.Web.UI.Page

End Class
At this point, the file Test.aspx should behave like a normal code behind file....

Hope this answers the question, as I wasn't sure exactly what the question was in the first place... ;)

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
  #6 (permalink)  
Old February 14th, 2006, 01:34 PM
Friend of Wrox
 
Join Date: Apr 2005
Location: Fort Walton Beach, FL, USA.
Posts: 190
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar,Chris:

Thanks for the help.

I think one should as a matter of habit, request the code behind page at the time of the page creation. Occasionally one will forget, like I do.

Imar's one comment "It's important that you name the file .aspx.vb, not just .vb" was the critical thing to make that particular code behind page nest correctly under its associated aspx page.

I did notice one discrepancy tho. After getting the code behind page created and properly nested, and doing the Page directive stuff, clicking above on the code behind page, for the page_events, I found that page-events was not offered in the drop down. Only the class name and "form" were presented.

I fixed the situation by manually coding a Page Load event handler on the code behind page and calling for a compile. Thereafter, page-events was offered.

Thanks again.

VictorVictor




  #7 (permalink)  
Old February 14th, 2006, 04:04 PM
Imar's Avatar
Wrox Author
Points: 72,035, Level: 100
Points: 72,035, Level: 100 Points: 72,035, Level: 100 Points: 72,035, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,079
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi VictorVictor,

Out of curiosity, I tried this again, but immediately after I change Public Class Test to Partial Class Test Inherits System.Web.UI.Page, I got the drop-downs at the top of the page with the page events.

Did you change the class and make it inherit from Page?

And I agree; it's usually much easier to let VWD do all this for you. However, you're likely to run into a page with in-line code that you want to incorporate in a Code Behind application. In that case, this knowledge may come in handy.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Female Mechanic Now On Duty by Sonic Youth (Track 3 from the album: A Thousand Leaves) What's This?
  #8 (permalink)  
Old February 14th, 2006, 04:55 PM
Friend of Wrox
 
Join Date: Apr 2005
Location: Fort Walton Beach, FL, USA.
Posts: 190
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar:

Yes, I made the page inherit from system.web.ui.page.

I did a number of things, experimenting around and forget the exact order in which I did them. The main thing is exactly as you said, I can now remove code in script blocks and make a code behind partial class.

Chris U. mentions somewhere that one does not have to change the class designation from Public to Partial, and I was testing the effect of leaving it Public when I implemented your comment about being sure to include the .aspx. name level in the code behind class.

I'm converting a 60 page site that I wrote with the Beta 2 of VS 2005 to ASP.NET 2 Express and have a lot of <script> blocks to unwind. (People assure me that I won't have to convert, again, from the Express to the full blown ASP.NET2 (which I cannot afford anyway).

Again, thanks for your help. I've moved on to Roles and Profiles, in the book.

VV



  #9 (permalink)  
Old March 30th, 2006, 12:47 PM
Registered User
 
Join Date: Mar 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This information has been very helpful. I wanted to point out a couple of other things I noticed:

1) If the code-behind file does not have the same initial filename as the page, the files will not get nested in the window. The page, however, will still run properly.

2) If you publish a web site and allow the precomplied site to be updateable, you may occassionally have some problems. I am not sure if it has to do with some sort of caching mechanism within VS or why it really happens, but one minute, the members of a class come up just fine in the code behind and the next minute I have numerous "<name> is not a member of <classname>" errors. I also found that if those pages where open, I did not see the errors - I only saw a message that stated the maximum number of errors has been exceeded. I had to close all the files and then run a build to see the actual error messages. The fix is easy and it is actually the reason I came across the first item. Simply rename the code behind file, all references to it and the name of the partial class. After a build, everything is fine. I then went back to the original names and did another build with no problems.

Hopefully, these issues will be addressed in a SP.

RC
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chap 2 Solution broadiea BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 January 19th, 2007 05:56 PM
Appendix B pkara2006 ASP.NET 2.0 Basics 0 August 20th, 2006 09:38 PM
Chap 10 FileSystemObject Problems orbbital BOOK: Beginning ASP 3.0 2 April 20th, 2005 10:19 AM
Page 638 Connection to SQL Server Jerry Obrien VB.NET 2002/2003 Basics 4 January 31st, 2005 05:42 PM



All times are GMT -4. The time now is 01:04 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.