View Light

My ASP.NET page is running the wrong code-behind?!

Ran into a strange thing yesterday: I had an ASP.NET project with an existing page with very similar functionality to another page I needed to create, so I just copied the existing page  using the Visual Studio Solution Explorer, renamed the file to the new filename.aspx needed for the new page, changed the Class name in the .vb code to match the new filename (Otherwise the class name of the copied file would collide with the class name of the original file I copied from.), and then tweaked the code to change the little bit of the code (and a little bit of the HTML part as well) that needed to differ in functionality from the original.

I went to try it out in the browser. The HTML part of the page looked correctly modified, but the data being displayed was from the database table being accessed by the original file.

Did I forget to change the table name? Probably, I've done dumber things...  I bring up the new code in the editor, and see that I did indeed change the table name in the query. So what in the??

I added a quick Response.Write("This is a test") to the code, do a Rebuild Solution, and try it again in the browser. Same thing, and the "This is a test" does not appear.

Strange. Very strange.

Next thing I try I don't think will work: I add a Response.Write("This is the original code") to the original .aspx file that I copied from. Rebuild. Try again. The "This is the original code" shows up in the COPIED code that should say "This is a test".

Now I'm really stumped. I look through the project using Windows Explorer and open individual files using UltraEdit rather than Visual Studio. I'm a little worried about the .resx files, they contain stuff like "PublicKeyToken=b77a5c561934e089" that I'm not all that sure what they are supposed to mean. Is it possible that there is some kind of unique GUID identifying the original file is tagged into the .resx file for the copied code, and it will always point to the original? I hope not.

I'm also working with my code checked into Visual Source Safe. Is there something confused there since the original code I copied from was already checked in? Is Source Safe unable to differentiate between the two files? I check everything out, delete the .resx file for the copied code, rebuild the project, check everything back into SourceSafe, then try again. Same thing. Still could be Source Safe, but unsure what to go on.

It's the end of the day, so I leave myself some notes on my theories about what might be wrong so I can pick it up in the morning. Gettting away from a project is usually the best thing to do when you're stuck. That holds for this problem as well.

I return in the morning. Re-read my notes. Think about it for a while. Finally get to looking at the .aspx (not the .aspx.vb) code in UltraEdit again. (Sometimes it does some good to get away from all the hand-holding that Visual Studio does for you in cases of weirdness like this.) I finally find it: At the very top of the .aspx file, there is the line that looks like:



That last "Inherits=" part is the culprit: It's still pointing to the Class name for the original file I copied from. I change that to the new class name for the copied file and try again and everything works!!

So, a quick checklist on how to work with a copy of an existing ASP.NET page:

1. In the Visual Studio Solution Explorer, right click on the original file, select Copy. Right-click on the containing folder and select Paste.
2. You'll end up with a file called "Copy of OriginalFilename.aspx". Right-click on it and select Rename (Or just select the file and press F2) and change the filename to whatever you want it to be.
3. Select the new file and pick "View Code". Change the class name to something else, typically the file name minus the .aspx extension.
4. Select the new file and pick "View Designer". When that pops up, select the HTML tab at the bottom left of the designer. On the top line, change the "Inherits=" to inherit from the new class name.
5. Save the file and you're ready to start modifying it any way you need.

Reply
This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available in our efforts to advance understanding of environmental, political, human rights, economic, democracy, scientific, and social justice issues, etc. We believe this constitutes a 'fair use' of any such copyrighted material as provided for in section 107 of the US Copyright Law. In accordance with Title 17 U.S.C. Section 107, the material on this site is distributed without profit to those who have expressed a prior interest in receiving the included information for research and educational purposes. For more information go to: http://www.law.cornell.edu/uscode/17/107.shtml . If you wish to use copyrighted material from this site for purposes of your own that go beyond 'fair use', you must obtain permission from the copyright owner.