The relationship between code behind class and aspx generated class - c#

I was reading a book says that "a Web Form’s code-behind class was the base class for the class generated by the runtime from the .aspx file itself, as the picture below shows:
I don't know how the generated class looks like, as I only deal with aspx.cs which is the code-behind class.
So let say I have a .aspx file that have some controls like textboxes, do the generated class generated by .aspx file contains Textbox class? and how this generated class inherit the code-behind class, I'm really confused, can anybody post an example code of generated class generated by .aspx file?

The generated classes are stored here:
%windir%\Microsoft.NET\Framework\version\Temporary ASP.NET Files\root
Replace the version in the above path with the version of your framework. There will be many files in there but you can delete them all to start afresh. Then restart the web application in question and you will see a brand new folder (with a numeric name) will be created in the root folder. Inside the folder you will see many files but if you sort them by type, one of the CS Files will be the one generated for the .aspx page. The name will not be a friendly name so you will have to keep opening them and search for nameofpage_aspx. For example, if the aspx page is named About, search for about_aspx. If you do not find it, search for it in another file and one of them will be for about_aspx.
You may also look for the first line in the file which starts with #pragma checksum and it will have the name of the .aspx page. Not all files will have this line.
In those CS File types are the generated code which will be used by asp.net and you can examine its contents in more details if you want. Please do not ask me to explain the details of what is in those files but I am sure if you search online, you will find info on that.

Related

Publish Web Site + reverse engineer and get back .aspx?

Actually those 2 links talks about the .net Reflector. But my problem is that when we choose the PUBLISH WEBSITE option for a WebSite in Visual Studio, and then in the next window that comes up, uncheck all the 3 options which are
Allow this precompiled site to be updatable --> Not Checked
Use fixed naming and singe page assemblies -->Not Checked.
Enable strong naming on pre-compiled assemblies --> Not Checked.
In the location where the files are placed , we can see .aspx file which has the content This is a marker file generated by the precompilation tool, and should not be deleted!.
Now in the current scenario that I can facing, some person has published the website , using the PUBLISH WEB SITE option and then deleted the existing dotnet code. Now if I reverse engineer using the .Net Reflector, I get back .aspx.cs files, but I cannot get back the .aspx files.. Actually the .aspx file contents are represented as a class which inherits from the Page Class of dotnet. Below is the sample snippet what I get from the reflector
public class HomePage : Page, IRequiresSessionState
{
// Fields
protected HtmlForm Form1;
protected Literal InvalidPortalHeader;
protected Literal InvalidPortalText;
protected Table Table1;
.................. Something like this.
If code is generated in this format, then I will have to add new controls to the webpage in this format. Is there a way that I can get back the .aspx file also (not only the .aspx.cs file), so that I can add new controls like Label, Textbox at design time using my Visual Studio.
Is there any idea to get that .aspx file with original code?

Codebehind file doesn't recognize aspx-controls

I was given a webproject written with aspx/c#. When I loaded it into Visual Studio 2010, I got many error telling me that some controls in the code-behind files do not exist in the current context.
I checked for the common pitfalls, like wrong code-behind file name, missing runat-attribute, restart VS, reload project, yet nothing resolves the error.
What else can I do to check where the problem is?
Try to change the code-behinde to CodeFile="where code locate", then it probably will work.
inherit class for the markup file and code behind file should match ,and also make sure if these controls are not third party and may need referencing their dlls or something .If these controls are user controls(ascx) controls , make sure they are using the correct tagname in the register line at the top .
Cudos to #03Usr the following link brought the answer:
ASP.NET controls cannot be referenced in code-behind in Visual Studio 2008
Deleting all designer files and converting the project als web application brought the designer files back, effectively making the controls referenceable again.
This is a really annoying hack but if you delete the control from the aspx page and then save the page, then paste the control back onto the page and save again the code behind then seems to recognize the control. I keep running into this problem whenever I change an attribute like the ID.
In my case, the aspx file was inside a folder named XYZ, and the class name was ABC. So ASP named the C# file's class name as XYZ_ABC
After renaming the file to ABC, the error was fixed
Hope it helps!

Creating prepopulated aspx files with c#

My objective is to write a program that can create folders and within those folders create Default.aspx pages. The Default.aspx pages must read information from the database to populate the page using their containing folder name as a parameter to call the database.
Create the folders is the easy part, but I'm not sure how to go about writing information to .aspx files.
The goal of this project is so a real estate company can create pages for featured properties (through a cms), each featured property getting it's own folder/page.
Any feedback would be great.
-Aaron
Rather than physically creating the .aspx files, I think it makes more sense to have 1 .aspx file, which is passed querystring values to return different content from the CMS, depending on its value.
Furthermore, you could use URL Rewriting to make the file appear as if its in a physical location, within a folder.
For example:
/Property.aspx?agent=EstateAgent1&name=Property1
Can be rewritten to:
/EstateAgent/Property1.aspx
If you have to generate folders and files, I'd recommend simply generating HTML files, as theres no need for the file to be dynamic, or ran at the server
I would create a template ASPX page, and copy that template into the new folders. Within the template, define the layout and add all of the necessary controls for displaying the data. Doing it this way, you can change your template as needed for changes, and after the template is copied to the folder you can customize it as needed.
I would be remiss not to warn you that this approach will be very difficult to maintain though. Unless there is some requirement I'm unaware of, I don't know why this approach would be favorable.
Hope this helps.

Why doesn't this inherited view render?

I tried the following:
I have a shared library (.dll) that contains these files:
Views
Search
PowerSearch.aspx
PowerSearch.aspx.cs
PowerSearch.aspx.designer.cs
The PowerSearch.aspx file contains my html code.
The PowerSearch.aspx.cs file contains this:
using System.Web.Mvc;
using CommonProject.Web.Shared.Controllers;
namespace CommonProject.Web.Shared.Views.Search
{
public partial class PowerSearch : ViewPage<SearchViewData>{}
}
And the designer I catually don't even care about cause it's not used anyways.
Nothing fancy, just a strongly typed view.
I basically pulled an existing, working view out of my asp.net mvc project, put it in a seperate library and changed the namespace to contain the word "Shared" as in "this will be shared amongst several mvc projects".
Then in my original asp.net mvc project I created the same structure, only now the aspx page contains nothing but the asp #Page rule.
The matching cs file contains:
using System.Web.Mvc;
using CommonProject.Web.Shared.Controllers;
namespace CommonProject.Web.DRE.Views.Search
{
public partial class PowerSearch : CommonProject.Web.Shared.Views.Search.PowerSearch { }
}
There are no compile errors and no run time exceptions either. There is only a huge blank page...
Anybody got an idea?
Make sure the assembly (dll) is in your bin folder that you are referencing. Also try adding the assembly namespace to your web.config otherwise your View will not be able to find the inherited page.
<pages>
<namespaces>
<add namespace="CommonProject.Web.Shared"/>
How much of a struggle was it to try to add an ASPX page to a class library?! It's not in the default list when you select 'Add New Item...'.
I know it's OK to store Controllers and Models in a separate library DLL/assembly, but I'm pretty sure it's not as trivial to store or share the Views. The problem is that the default view engine is looking in a specific folder on disk for the view (~/Views/Controller/ViewName.as[pc]x or ~/Views/Shared/ViewName.as[pc]x). With a library DLL, the compiler doesn't really have any idea of what it can do with your ASPX file. It's not code, so unless you have a 'Build Action' set, it's just going to ignore it. There are various 'Built Actions' but I think your only options are 'Copy' and 'Embed As Resource'. Copy isn't going to copy to the folder that you need it to (the Views folder in your ASP.NET MVC web project), although you could possibly write a build script or 'Custom Tool' that did that for you (with a bit of work).
Hammett (of Castle Monorail) fame (and now an MS Employee) came out with a sample that allows you to store Views inside library assemblies using a custom VirtualPathProvider class that is able to dig into the DLL and pull out the View (embedded as a resource). The sample application is just a concept right now, so you might hit some roadblocks, but it appears to work and looks like an exciting direction. You can find it on his blog here: MEF and ASP.NET MVC sample. Download the code and do some exploring.
This blog post ASP.NET MVC Plugins is not by the same author as the one above, but it gives another examination of the topic and points to another post here on StackOverflow where a similar question was asked: Using VirtualPathProvider to load ASP.NET MVC views from DLLs.
I've seen a post from Phil Haaaaaaaaaaaaaaaaaack on storing views in the database. It's using Ruby scripts instead of Web Forms, so I'm not 100% sure if you could adapt his sample to fit your needs or not. Check it out here: Scripting ASP.NET MVC Views Stored In The Database.
What does the source look like for the .aspx file? Make sure you are inheriting your view in the page directive like this:
<%# Page Title="" Inherits="CommonProject.Web.Shared.Views.Search.PowerSearch" %&>
Also, if you don't want to specify the whole namespace in your page directive, you can add this to the <namespaces /> section in your web.config:
<add namespace="CommonProject.Web.Shared.Views.Search"/>

Best way to copy a web form page within a project?

So let's say you wanted to make a copy of a Web Form page within a .Net Project.
Is there an easier way than:
Copy Source Page
Page Source Page within project to get new page
Exclude Source Page
Rename code behind class for new page
Add Source Page Back
Sometimes I miss something obvious is there a better way to do this? I know the next question would be "Why are you copying code within a project instead for reusing it?" Let's just say that's a secret;).
I do this:
Select the original ASPX file in solution explorer
Ctrl+C followed by Ctrl+V (quick copy paste)
Rename new ASPX file (let's say NewFile.aspx)
Rename code-behind class name to NewFile
Rename Inherits attribute of Page directive within HTML to end with 'NewFile'
(Optional) If you moved the page into a different folder, you'll need to update the Namespace references in the HTML's Page directive as well as in the code-behind.
Create new page via "Add New Item"
Copy original markup (minus Page declaration) and paste into new page
Copy code from original code-behind and paste into the new code-behind
Can you make that form a User Control, and then insert it as needed? Then you can save yourself the trouble of editing every instance of it that you copy.

Categories

Resources