ASP.NET controls not recognised in code behind - c#

I am getting the
"The name 'ASP.net control' does not exist in the current context"
Error message. Guys, can you please help me find out the cause of this and how to remedy it!
Am I missing something here??
<%# Page Title="Amatola Water Activities" Language="C#" MasterPageFile="~/Site.Master"
AutoEventWireup="true" CodeFile="~/Default.aspx.cs"
Inherits="AWActivitiesWeb2.Default" %>

There could be several reasons behind this error, Looking at the <%# Page %> directive that you shared, you have specified the attribute as CodeFile="~/Default.aspx.cs" Which might be causing this issue because if you specify a CodeFile attribute, the designer.cs file does not regenerates itself. Either you manually specify each control you add into the designer.cs file or you should change the CodeFile attribute to a CodeBehind attribute.
The other issue could be with the control itself, if you won't specify runat="server" on the attribute then you won't get its reference in the Code Behind.
So, Modify your page directive with this line:
<%# Page Title="Amatola Water Activities" Language="C#"
MasterPageFile="~/Site.Master" AutoEventWireup="true"
CodeBehind="~/Default.aspx.cs" Inherits="AWActivitiesWeb2.Default" %>

Related

There can only be one 'page' directive - How do I fix this?

I am pretty new to c# and asp.net and I've encountered an error on a page with the error stating that
"There can be only one 'page' directive."
The code is as follows:
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="AddUser.aspx.cs" Inherits="OEPD.AddUser" %>
I have this page linked into another page using the
<!-- #include file="~/Admin/AddUser.aspx --> function.This page is also using the MasterPage
<%# Page Title=" Admin Home" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="AdminHome.aspx.cs" Inherits="OEPD.Admin.AdminHome" %>
How do I fix this? So it not only links in, but works without errors?
Thanks in advance.
p.s this is my first question so I apologise if it seems a bit vague.
You shouldn't use #include file in asp.net, because it's a classic asp mechanizm and there are better ways of doing such things in new version of framework. In order to reuse your code in many pages you could rewrite your "inner page" to user control or your "outer page" as another master page. Here is more info about this topic: ASP.NET equivalent of server side includes

Content page not able to access controls after changing the name of the page

I have a web application project in VS2013 with a Master Page and some content pages. My master page header is:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="MasterPage.master.cs" Inherits="MyProject.MasterPage" %>
I then right click on the Master Page in the solution explorer and select "Add Content Page". This produces a content page like the following for me:
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="True" CodeBehind="WebForm1.aspx.cs" Inherits="MyProject.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
</asp:Content>
What I do then is right click this "WebForm1" in the solution explorer and change the name to "Login"
This changes the header in the content page to:
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="True" CodeBehind="Login.aspx.cs" Inherits="MyProject.WebForm1" %>
Looks fine except it still says Inherits="MyProject.WebForm1" despite that no longer being the name of the content page.
So I figure, "OK I can just change Inherits="MyProject.WebForm1" to Inherits="MyProject.Login". Big mistake, because now I cannot reference any of the controls like TextBox, Label, etc. in my code behind file. I get a list of errors all along the lines of:
The name 'controlname' does not exist in the current context
So it looks like my page can no longer find the class for the controls correctly.
I tried deleting Login.aspx.designer.cs and then recreating it by going to Project -> Convert to web application but that did not work.
So long story short, my code works fine if I use the Inherits="MyProject.WebForm1" despite my page now being called Login. I also tried this with all my other pages and it is the same story.
Is this some kind of bug? Or is my method of adding content pages the problem?
Most likely in your code behind you have this:
namespace MyProject
{
public class WebForm1
This is what your page inherits in terms of the web site prokject, and that is what should be in the "Inhertis" attribute. Note that full class name, including namespace, is expected there.
So if you want to rename your page file - make sure you rename the class as well. Visual Studio does not do this automatically.

How to configure MasterPage to Inherit class file

I have a Master page which needs to inherit from a class (MasterPage2.cs) stored inside "App_Web_yht1mma2" folder.
In the master page, I have set the code as:
<%# Master Language="C#" AutoEventWireup="true" Inherits="MasterPage2,App_Web_yht1mma2" %>
When I debug this page, I receive the following error:
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: Could not load the assembly 'App_Web_yht1mma2'. Make sure that it is compiled before accessing the page.
Source Error:
Line 1: <%# Master Language="C#" AutoEventWireup="true" Inherits="MasterPage2,App_Web_yht1mma2" %>
Line 2:
Line 3:
Source File: /MasterPage.master Line: 1
Kindly advise me on how to correct this error.
Thank you for your help but I managed to figure it out through assistance from a friend.
<%# Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage2" %>
or
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="MainMasterPageWeb.Master.cs"
Inherits="App_Web_yht1mma2.MasterPage2" %>
Both ways are possible solutions

Access UserControl property on MasterPage from derived page? This used to work!

This is driving me crazy. In the past I have been able to have a master page, put a user control on that page, and create a read-only property referencing that usercontrol, and access the usercontrol and all its properties from the derived page. Now I am getting this error:
The type ‘XXXX’ is defined in an assembly that is not referenced. You must add a reference to assembly 'App_Web_2zw4yn55, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
I looked at older projects and see that I was able to do this. I am using .Net 4.0 now, but not sure that is an issue.
My page declarations look like this...
Master Page:
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Main.master.cs" Inherits="MasterPages_Main" %>
<%# Register src="../UserControls/WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %>
<uc1:WebUserControl ID="WebUserControl1" runat="server" />
Code Behind for Master Page:
public UserControls_WebUserControl TheWebControl { get { return this.WebUserControl1; } }
Derived Page:
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPages/Main.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%# MasterType VirtualPath="~/MasterPages/Main.master" %>
<%# Reference VirtualPath="~/MasterPages/Main.master" %>
Code Behind for Derived Page:
protected void Page_Load(object sender, EventArgs e)
{
Master.TheWebControl.Pagetitle = "Hey";
}
If I put a reference, in the page declarations of the derived page, for the usercontrol it works, but I shouldn’t have to do that. I am not sure what is going on. I have never had to do this before. The only thing I can think of is that my web.config was setup differently, but even in my old pages I am not seeing any direct references to the usercontrol.
UPDATE:
I may have been mistaken. Looking back at the older code again, I did, indeed, add a reference in the page directives to the usercontrol on the derived page.
Perhpas there is anohter way of doing this without having to add the directive?
I was mistaken. Looking back at the older code again, I did, indeed, add a reference in the page directives to the usercontrol on the derived page.

Parser Error Message: The file '/TestSite/Default.aspx.cs' does not exist

Short story.
This site was created by a friend of mine, who did not know that much C# or asp. And was firstly created in VS 2k3. When i converted it to VS 2k8 these errors started to crop up, there was also other issues with compiling that i managed to sort out ( Seemed to be released to VS 2k8 wanted design files )
Error message gotten:
An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: The file '/TestSite/Default.aspx.cs' does not exist.
Source Error:
Line 1: <%# Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="GuildStats._Default" %>
Line 2:
Line 3: <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="Server">
Defaults.aspx.cs
namespace GuildStats
{
public partial class _Default : System.Web.UI.Page
{
Defaults.aspx
<%# Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="GuildStats._Default" %>
Site.master.cs
namespace GuildStats
{
public partial class Site : System.Web.UI.MasterPage { }
}
Site.master
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="GuildStats.Site" %>
In Default.aspx change CodeFile to CodeBehind. You'll probably have to do the same for the Site.master.
See: CodeFile and Code-Behind
Web Site projects use CodeFile, Web Application projects use CodeBehind.
CodeFile requires the source file, it is compiled on the fly when the page is loaded, CodeBehind requires the compiled code.
My guess is that your problem was created when you changed your project type from a WebApp to a Web Site or vice-versa. If you do this, you have to manually change the directives in the existing files, new files will have the right directive automatically.

Categories

Resources