Broken link between aspx and aspx.cs files - c#

I've had the same problem a couple of times with different ASPX pages after renaming them and I am surprised that I can't find someone else with the same problem on stackoverflow.
When I run my ASP.NET C# project, the debugger gives me a message like this one.
Error 5 The name 'txtTitle' does not exist in the current context
It seems that the aspx and aspx.cs files at no longer bound. The only fix I have found for this is to recreate the page and copy/paste my code.
Any idea how to fix this without recreating the whole thing?
Thanks

The code file contains a partial class that is referenced in the ASPX header declaration. Both file name and the actual class in the ASPX header have to match for this to work.
<%# Page Title="TestPage" Language="C#" AutoEventWireup="true" CodeFile="TestPage.aspx.cs" Inherits="TestPage" %>
In your case probably the class name does not match. Check if the class name in the codebehind .cs file matches the name after Inherits.
The concept of partial classes used in ASP.NET is detailed here.

I am surprised at this occurring I regularly rename Silverlight user controls with no harmful side-effects.
Are all of the references to classes being renamed in the aspx page and code behind ?
You might try using this util to relink them.

This can also be caused by the whatever.aspx.designer.vb file being dependent upon whatever.aspx.vb instead of whatever.aspx. If this is the case, it should show incorrectly in the file tree when you expand the group of files in Visual Studio. To fix it, simply exclude the affected files from the project and then re-include them - the faulty project config file should be regenerated and it should now work correctly.
(Same theory for C#.)

I've had the same problem and noticed that sometimes, but not always, the CodeFile setting in the .aspx page is not updated when the class is changed. If this is the same problem you have, you can change it manually:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="MyClass.aspx.cs" Inherits="MyClass" %>
^^^^^^^

Related

How to duplicate a master page in asp.net

I'm trying to skin a 3rd party asp.net application and don't boast to have any knowledge with MS web technologies.
One page design has a considerably different underlying html structure that can't be achieved with pure css so I need a new master file for this page.
I've naively duplicated a file called Main.Master renamed it Login.Master and changed the reference in the Page file that extends Main.Master to Extend the new master page file, e.g:
<%# Page Language="C#" MasterPageFile="~/Login.Master" EnableViewState="true"
AutoEventWireup="true" Codebehind="Login.aspx.cs" Inherits="xxx.login"
Culture="auto" meta:resourcekey="PageResource1" UICulture="auto" %>
In Login Master I've not changed a thing, as all the logic should be identical, I just want to change the structure of the HTML.
With the small change mentioned above I get several parser errors along the lines of:
Parser Error Message: The resource object with key 'somekey' was not found.
When I delete the reference to said error, it causes a new parser error further down, and so on and so forth.
I've also tried not having a master page for the page in question but that throws up a whole new collection of errors.
To make things worse, it's on a remote server and I don't have access to visual studio so would prefer a solution that wouldn't require compilation if at all possible.

Issues with code behind generation

For some odd reason, I can't get my page's code behind to generate, the file has been created but none of the controls seem to be getting generated in..
There are no warnings/errors coming from VS when I am trying to generate this file, this has worked in the past but suddenly it stopped working and as far as I know nothing has changed apart from I have installed .NET Demon which I have tried disabling fully but this doesn't appear to change it.
I have spent a good few hours looking into this issue and all I have found in terms of solutions is to delete the .designer.cs file and then converting the .aspx file to a web application but this does not fix my issue. I have also tried to recreate the page but no luck either, I will put below a few bits of code that may help.
Generated file namespace:
namespace Web.admin.reports {
<%# Page Language="C#" Inherits="Web.admin.reports.productpromotionsummary" Title="Promotions Summary" CodeBehind="~/admin/reports/productpromotionsummary.aspx.cs" AutoEventWireup="True" %
If you need anymore information please feel free to ask

Where Controls Declarations in a website project are put

I have website i cant access the controls in the code-behind
i drag controls and add them to ASPX file and when i try to access them i cant.
i tried opening the website using:
Visual Studio 2012
Visual Studio 2010
when i even double click on a button it creates its event in the code behind but i still cant access the control!
there is no designer.cs file since this is a web site any idea how to fix it ?
If memory serves, the markup is the designer for a page that lacks a designer file. You can observe this behavior in classic ASP pages that have been ported to .NET.
The compiler simply parses the page, looking for a tag that includes runat="server" and generates a server-side control for it. If there's no ID, one is generated for it. Further, if AutoEventWireup="true" is set at the page level, the events will be automatically connected to these controls, as if by magic. It's all just compiler voodoo, if you ask me, but rather convenient compiler voodoo.
But that's an educated guess, based on experience, and I'm sure someone with far more experience with ASP.NET internals can provide more riveting details. (Jon Skeet, et al.)
Make sure your .aspx file specifies its codebehind file:
<%# Page Title="" Language="C#" CodeFile="YourPage.aspx.cs" Inherits="Content_YourPage" %>
If it is a website then websites don't have designer.
But if you need the designer's then try to convert it to web application

GridView to Excel ASPX in SharePoint

I have an ASPX file which I am running in SharePoint that has a GridView which I am attempting to export into an excel spreadsheet. I know the code I have for the export is correct; however, I am getting this error:
RegisterForEventValidation can only be called during Render();
I have done some research and have found a solution, which might work with normal ASPX pages created in VS with a CS code behind class, which is setting the EnableEventValidation to false, and I am getting a Parser error when I attempt to use this solution on my ASPX page.
<%# Page Language="C#" AutoEventWireup="true" EnableEventValidation = "false"%>
Is there any other way to allow this, or any other workaround to my problem using just the ASPX page without the code behind? All of my C# code is within the head and has to be in order for me to run it in my SharePoint environment. 12 hive storage of the CS class is not an option for me. Also, I am not wanting to change anything in my web.config folder if possible.
Thanks
Try creating it as a Web Part - this is the more common approach for this type of scenario in SharePoint.
In SharePoint, all customized pages are run through the Safe Mode Parser which prohibits inline code. Also, this parser does not allow adding controls to pages which have not been marked as "safe".
To get around your issue, you might want to look at "Application Pages" which are added to the \LAYOUTS directory. They reference the master page and can have inline code, but they can't be customized. They are compiled into a single assembly DLL:
Creating an Application Page in Windows SharePoint Services 3.0

Page Directive Error After Copying Default.aspx From Another Project?

In my web-application (asp.net, C#) in solution explore I copy and paste the default.aspx page which is in the other project. When I run the default.aspx page it is giving error like this
<%# Page language="c#" Codebehind="Default.aspx.cs" AutoEventWireup="false" Inherits="Default" %>
Parser Error Message: Could not load type 'Default'.
Can anyone tell me how to fix this?
In addition to making sure that the class names match, as stated in an earlier answer, there are important differences between the "Codebehind" and "CodeFile" directives. The former, which you're using, is the older, .NET 1.1 way of doing things. In that case, in your code behind you have to declare instances of all of the controls on your page that you need to access, and you shouldn't use partial classes.
For CodeFile, you must use partial classes, and you don't need to declare any of the controls; that work is done for you in the other part of the partial class.
If you're porting an existing page, you need to understand which model it needs, and use the corresponding declarations in both the code behind and the Page directive.
What is the class in your .aspx.cs file? Should be something similar to:
public partial class Default : System.Web.UI.Page
Sometimes it's created as _default

Categories

Resources