I just inherited a web site that was created by a designer. The site was originally created with all *.html files. The designer renamed all the *.html files to *.aspx files. Hence there are no aspx.cs files created. I pulled the site into a new VS2012 solution. My question is, is there a way in VS 2010 to automatically create the code behind files for a an existing stand alone aspx file?
I don't know of an automated way to do this, but if there is no server side code in the existing *.aspx files then it should just be a case of adding the .cs codebehind files and then wiring them up in the <%# Page tag like so:
<%# Page Title="YourPageTitle" Language="C#" AutoEventWireup="true" CodeBehind="YourPage.aspx.cs" Inherits="YourNamespace.YourPage" %>
Note: This will not create the YourPage.aspx.designer.cs file. (I usually delete these anyway as they cause merge issues - i find it easier to add the controls i need to reference to my code-behind file manually.)
The other alternative is to just create a new "Web Form" for each page with the correct names and then copy and paste the existing markup into them. If you do have server code in the existing *.aspx files then you will need to manually copy it to the code-behind.
Based on what I found here: http://forums.asp.net/t/1229894.aspx/1
Right click on your solution explorer.
Add New Item -> Class File.
Name the file as the name of your aspx eg: Default.aspx.cs
When it asks you the file should be in app_code click "no".
In your aspx in page attribute add
AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default"
Similarly in your class file that you just added remove everything. Your class should look like this:
//all namespaces go here
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
After you add the new .cs file, you may want to see the file look like a codebehind file (indented, icon, etc). To do so:
Unload the project
Edit the project
Find the new filename (file.aspx.cs) in the section with files.
Add an xml node for DependentUpon.
Save and Close the project
Reload the project
For a file Profile.aspx.cs, the xml should look something like this:
<Compile Include="Profile.aspx.cs">
<DependentUpon>Profile.aspx</DependentUpon>
<SubType>ASPXCodeBehind</SubType>
</Compile>
In Visual Studio 2012: Right click on the project --> click Add --> click Web Form --> Copy the content of your original aspx file into the new WebForm aspx --> delete the original aspx file --> Rename the new one to anything you want.
Now you should have a new aspx file with a code behind file that is ready for use
To save yourself from the drama of manually editing the project file like David Frette details, I suggest you remove the file from the project and create a new file with the same name with a code-behind. Then copy-paste the contents of the original aspx or ascx to the new files.
Related
So this code is in a file login.aspx which literally has some html mnarkup and the below code, so where does this code get loaded from? Where is the App_Web_login.aspx.d7a6dcf1 file located ?
<%# page language="C#" autoeventwireup="true" inherits="stadm_login,
App_Web_login.aspx.d7a6dcf1" enableeventvalidation="false" theme="Niko" %>
The code is contained in a file that is associated with its page. In Visual Studio if you load the project you will see a caret next to the aspx page. Click that and you will see a file associated with that page. That is the code file, or code behind, for the login page.
When the project is built, the code for each page is wrapped into a dll file or code package for the entire project. What you see for the inherits in the page is the base class for the project and also the inheriting of the page class.
So if my project is called FooProject, then at the top of each page where it says inherits, you will something like this (the _base class .page class):
inherits=_FooProject.Login
I found the following post: Render partial from different folder (not shared)
However this isn't working on my page.
The file structure is as follows:
>Areas
>>NewNavigation
>>>Views
>>>>Shared
>>>>>MyCustomView
>Views
>>Shared
>>>SignInLink
I want MyCustomView to be able to render the partial found in: views/shared/signinlink
<% Html.RenderPartial("~/Views/Shared/SignInLink.ascx"); %>
Cannot resolve partial view
Try <% Html.RenderPartial("SignInLink"); %>, as the mvc runtime will search the shared directory (this is built in):
... will search the partial view in different folder:
The /Views/Home folder a file named “_MyPartial” (With aspx, ascx,
cshtml or vbhtml extension). The View/Shared folder a file named
“_MyPartial” (With aspx, ascx, cshtml or vbhtml extension).
http://coding-in.net/mvc-3-organize-your-partial-views/
I have Website project, which contains some .ascx and .aspx files. I have added new element <asp:TextBox ID="tb1" runat="server" ... /> in .ascx file and I have wrote some code in proper .ascx.cs file using this element: tb1.Text = "SomeText";. When I compile this project I recieve following error: The name 'tb1' does not exist in the current context.
How can I force to refresh markup of .ascx page? I use Website project and I cannot to change its type to Webapplication.
UPD: I have Website project, which has NOT .ascx.designers.cs files. And I cannot change type of my project to web application.
Unless there's something else happening here, it sounds like the designer.cs file might be out of sync. Try cutting and pasting the control back into the markup, or go into the designer file for the user control and add the TextBox manually:
protected global::System.Web.UI.WebControls.TextBox tb1;
It seems like your design file is not connecting with your code behind file.
Can you confirm if you are defining it as follows,
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="yourcontrol.ascx.cs" Inherits="CompleteNameSpace.ClassName" %>
I am getting a compilation error on a website. There is a repeater declared in the aspx file as follows:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="MyPage.aspx.cs"
Inherits="MyClass" %>
<asp:Repeater ID="rptMyRepeater" runat="server">
<ItemTemplate>
<tr>
<td>
…
And the class is defined as follows:
public partial class MyClass : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
rptMyRepeater.DataSource = GetMyDataSource();
rptMyRepeater.DataBind();
}
}
}
The problem I have is that rptMyRepeater is not recognised. Note that I copied these files in from another project, and so don't have a designer.cs file.
I came across this question which implies a "Convert to Web Application" would fix the problem. As I'm referencing a CodeFile rather than a CodeBehind, does this apply, or is there a better way? Is a designer file even necessary in this case?
If what you are saying is you don't have just the contents of designer.cs, add this to designer.cs:
protected global::System.Web.UI.WebControls.Repeater rptMyRepeater;
If you do not have a designer.cs file at all, add it to aspx.cs -i.e codefile- and it should work.
Simply this is the equivalent of what the designer file supposed to be doing, controls are not much different than class variables as I see.
A couple of things you could check:
Ensure that your .cs file is set to Compile in the properties
Try using CodeBehind instead of CodeFile
If your page class is inside a namespace then ensure it is fully qualified in the aspx file
If you are using a Web Application Project then right-click and Convert to Web Application
I have the following line in my master page:
<td valign=top runat="server" id="navBar">
And then in the master page code behind we reference it and do something with it:
public void HideNavbar()
{
navBar.Visible = false;
tdMain.Attributes["class"] = "MainWrapper";
}
But for some reason when we copied this master page from a Web Site Project to a WAP project, it doesn't know what navBar is. It's not referencable in code-behind anymore.
Does the designer file need an entry for this? And what would that possibly be if I have an id in a ?
Another thing you could try is to delete the designer file, then right click on your master page and click on "Convert to Web Application." That will force a re-gen of the designer file, and pick up the new controls that have gotten out of sync with the designer file.
This specific issue is on my list of top reasons that I don't like Web Application Projects.
The move to a WAP may have messed up the class for your master page.
Try adding a protected control decleration within your master page. In VB this is:
Protected WithEvents navBar as HtmlTableCell
I'm not totally sure this is correct, but it would be my first guess.