Best way to copy a web form page within a project? - c#

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.

Related

In VS 2019 - How to regenerate code-behind and designer files from an existing aspx page

I imported in my project Web c# an aspx page, for example, mypage.aspx, with HTML code. This page is very simple.
All I want is to generate the 2 files (in IDE) aspx.cs and designer.cs (mypage.aspx.cs and mypage.aspx.designer.cs).
I tried "Convert to web project" functionality but it does not work.
Any idea, please?
Of course, I read several solutions on this site but none worked (with vs 2019).
If there is no actual code in the original code-behind file then it's pretty easy to do. Create a new page of the same name. Then just copy and paste the markup from the original, taking care to leave the newly-created page directive as is.
If there was logic in your original code-behind then you're out of luck. You might try decompiling the original DLL and try to re-create the original code that way.

The relationship between code behind class and aspx generated class

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.

How can I embed or navigate to a page contained in another namespace

I have two projects in one solution. One contains a page (which is basically a complex control) and another project which is an empty XAML application. What I'm wondering is how to either embed the page into the empty XAML project's main page or at least be able to navigate to it.
Any ideas?
How about Frame.Navigate(typeof(MyControl.MainPage))?

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!

Loading html page from a template on the computer

I am working with a C# on visual studios.
What I would like to know is if it is possible to have a html template loaded from a directory on my computer. Then every time I load up my code it will load up the .html template automatically.
Yes there is a way, not suggested but exist. You can use this code to include the file, inside your asp.net page.
<!--#include file="template.htm"-->
What is dose is that is load this htm file and show it on the place that you have place this declaration. Also you must know that this file can not contain anything dynamically. This is a command code that comes from the old asp script code, and still work on asp.net
So I can not call it as template. If you wish to make something like a real template you must use a master page. Using a master page is far better, and you just copy paste your htm code inside the master page and there you have it.
relative: http://triaslama.wordpress.com/2008/11/19/how-to-include-file-in-aspnet-pages/

Categories

Resources