I am trying to add a codefile to my asp.net masterpage. I had it working properly on an individual page, but cannot do so on my master page.
In my aspx master page I have:
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Master.cs" Inherits="Master.Master" %>
Then I have a file called:
Master.cs
and the code below is:
namespace Master
{
using System;
public partial class Master : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("ALEX");
Response.End();
}
}
}
as far as I can see all my declarations are correct, but the error I get is:
ASPNET: Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl).
and the line:
Line 5: public partial class Master : System.Web.UI.Page
is in red. I have had this error before but I am sure I have all my code correctly, so am not sure why I m getting this error?
Maybe the master file needs a different setup, or it works differently on the masterpage?
When defining a master page code behind class, the base class to inherit is System.Web.UI.MasterPage, not System.Web.UI.Page.
Related
I have this error. How can I solve it?
CS0426: The type name 'Site1' does not exist in the type 'MasterPage'
Here is my masterpage code:
The other answer will most likely be the cause. However, I have also run into this error if you name your project a reserved name.
For example, if you name your project "Login" or "UpdatePanel" you will also get this error.
The fix is to name your project something else. For example "CompanyLogin" or "CompanyPanel".
You need to set your inherits attribute on markup of master page with your fully qualified class name . Saying that open "Site1.master.cs" and you will see your class name which inherits from MasterPage .if class is part of a namespace then you should set youtr inherits attribute with [Namespace].[ClassName]
For example in my sample project. My master page has following directive on top
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="SiteMaster" %>
And My Codebehind class (Site.master.cs) is : see SiteMaster is the class my master page inherits from. Asp.Net Form engine compiles markup into a class which derives from a base class i behind the scenes
public partial class SiteMaster : MasterPage
{
private const string AntiXsrfTokenKey = "__AntiXsrfToken";
private const string AntiXsrfUserNameKey = "__AntiXsrfUserName";
private string _antiXsrfTokenValue;
protected void Page_Init(object sender, EventArgs e)
}
I have been trying to link a a web form to be a able to access a variable in the master page. I did this before it worked. But now when I do it I get an error.
The code in Site.Master
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="Site" %>
The code in Site.Master.cs
public partial class Site : System.Web.UI.MasterPage
{
public string hi = "";
protected void Page_Load(object sender, EventArgs e)
{
}
}
The code in WebForm1.aspx:
<%# Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="WebForm1.aspx.cs" Inherits="WebForm1" %>
<%# MasterType VirtualPath="~/Site.master" %>
The Code in WebForm1.aspx.cs
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Master.hi="new"
}
}
When I hover over the hi, I get this message
Error in Visual Studio - "You can use navigation bar to switch contexts
Here is another image
Screenshot
If you guys could help me it will really be great
I had this same problem in an MVC application so the WebForm-CodeFile-CodeBehind suggestion didn't really apply.
However, I realized the new classes that I created had their "Build Action" set to "Content". Once I changed this to "Compile", everything started working again.
In your WebForm1.aspx, try changing CodeFile to CodeBehind. That's what worked for me.
Try adding manual the ID of the form into the designer "yourname".aspx.designer.cs, in your case I guess it should be WebForm1.aspx.designer.cs.
You'll see a reference line for every id you have. You need to add a line like this -> "protected global::System.Web.UI.HtmlControls.HtmlForm hi" if you added the form like normal html
or -> "protected global::System.Web.UI.FormView hi" if you added as an aspx element
I want to add two aspx pages for single aspx.cs file. Is it possible? I need to do this directly.
namespace WebApplication1
{
public partial class PROJECT2 : System.Web.UI.Page
{
}
}
PROJECT2 ASPX.CS should be used for two aspx pages.
Not truly supported by Microsoft
I discussed this with Microsoft and here is what I got from them.
After further investigation, here are the results. Due to the manner in which Intellisense works, we cannot support Intellisense for scenarios involving a shared code behind on the aspx.cs files. There are two different approaches that one can take to deal with this scenario. The option supported in VS is using either AppCode or UserControll for the common code elements and then calling those methods to achieve a common code base. The second option involves using the CodeBehind in the manner that you are currently using it (without Intellisense), and assuming that the code is correct with respect to both design pages, the code should compile correctly since it is an ASP.NET supported scenario. Thank you for your feedback.
So here what that means
Intellisense will not work with both the pages, but only with one page
your code will compile only if both the controls are on both the pages!
This really is against the idea of having a shared codeBehind file. My scenario will most likely be two slight different pages which uses same code behind. But for Microsoft, two slightly different pages, can not use the same codebehind file
Ideal Scenarios should be
Intellisense should pick controls in both the pages
Code should compile if the the control that is accessed is present in either of the two pages.
So here it is the solution that perfectly suited my needs (thanks again ps2goat for the hint).
My basic structure of two pages was:
[namespace A]
Page.aspx
Page.aspx.cs
Page.aspx.designer.cs
and
[namespace B]
Page.aspx
Page.aspx.cs
Page.aspx.designer.cs
(assume i do have far more than 2 pages)
I did need to remove the .cs and .designer.cs files while being able to refer to server controls declared in the .aspx page.
This could not be possible with standard inheriting from base classes, nor using master pages: they work well, but are completely unaware of the children ASPX server controls.
So, I created a generic class file
[namespace COMMON]
Page.cs
In this file, I copied the content of both ".cs" partial class and ".designer.cs" partial class (obviously taking care of changing namespace to the new one) of either of the original namespaces (they were identical in code).
In page.aspx file, codebehind mapping was updated from "Page.aspx.cs" and "A.Page" namespace to "Page.cs" and "Common.Page" namespace.
So, files changed from these:
[Page.aspx] (one instance for each namespace)
<%# Page Title="Page" Language="C#" MasterPageFile="~/Page.Master" AutoEventWireup="true" CodeBehind="Page.aspx.cs" Inherits="Project.A.Page" %>
<asp:Content ID="ContentB" ContentPlaceHolderID="cBody" runat="server">
<asp:TextBox ID="txbTest" runat="server" MaxLength="75"></asp:TextBox>
</asp:Content>
[Page.aspx.cs] (one instance for each namespace)
namespace Project.A
{
public partial class Page: BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
this.txbTest.Text = "Hello";
}
}
}
[Page.aspx.designer.cs] (auto-generated, one instance for each namespace)
namespace Project.A {
public partial class Page{
protected global::System.Web.UI.WebControls.TextBox txbTest;
}
}
To these:
[Page.aspx] (one instance for each namespace)
<%# Page Title="Page" Language="C#" MasterPageFile="~/Page.Master" AutoEventWireup="true" CodeBehind="Page.cs" Inherits="Project.COMMON.Page" %>
<asp:Content ID="ContentB" ContentPlaceHolderID="cBody" runat="server">
<asp:TextBox ID="txbTest" runat="server" MaxLength="75"></asp:TextBox>
</asp:Content>
[Page.cs] (one SINGULAR instance, made by the content of old .cs and .designer files)
namespace Project.COMMON
{
public partial class Page: BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
this.txbTest.Text = "Hello";
}
}
public partial class Page{
protected global::System.Web.UI.WebControls.TextBox txbTest;
}
}
Then I deleted each instance of page's .cs and .designer.cs files, leaving a structure as I needed, like:
~/A/Page.aspx
~/B/Page.aspx
~/COMMON/Page.cs
And it works like a charm!
Taken from Can ASPX pages share code behind file?
I am trying to dynamically create a user controls in Visual Studio 2012/ASP.Net/C# but my project does not recognise the type name of my user control when I try to use it in the code behind of any other aspx or ascx page. I've tried rebuilding it and also removing namespaces and it seems like it should just work but it doesn't!
The top line of my ascx file is this:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="AG_Controls_WebUserControl" %>
The corresponding codebehind looks like this:
public partial class AG_Controls_WebUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
I try to declare in instance of it in the code behind of a blank aspx page like this:
AG_Controls_WebUserControl test = new AG_Controls_WebUserControl();
And I just get :
Error 3 The type or namespace name 'AG_Controls_WebUserControl' could
not be found (are you missing a using directive or an assembly
reference?)
It's been a long day and I am pretty tired but this shouldn't be rocket science, but I can't see what the problem is. Help much appreciated!
You forgot to include the reference to the control in the ASPX page. You still need to include that reference if you are dynamically creating the control in the code behind.
<%# Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %>
I am trying to create BasePage Web.UI.Page that is inherited by my main page.
But when i create public class mypage : BasePage method Page_Load of this class is not loaded in page live cycle.
BasePage does not contain any Page_Load.
has anybody got a clue where can be the problem?
thanx
1) Add class to your project
public class BasePage : System.Web.UI.Page
{
}
2) Assuming that you want your Default.aspx page to inherit from BasePage class, modify Default.aspx.cs file in the following way:
public partial class _Default : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
System.Diagnostics.Debugger.Break();
}
}
3) Do not change line <%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %> in your Default.aspx file.
4) Press F5 to start debugging. You will be asked to enable debugging in your web.config file. Press OK. You'll be stoped inside of Page_Load method, which means success.
I suspect your problem is that the event isn't actually called Page_OnLoad in the class, it's OnLoad. Here's how you want your class to look:
public class MyPage : Page
{
protected override void OnLoad(EventArgs e)
{
// Your code
base.OnLoad(e);
}
}
More detailed tutorial here.
Make sure the AutoEventWireup attribute is set to true. More information: http://msdn.microsoft.com/en-us/library/59t350k3%28VS.71%29.aspx
If you override OnLoad on some of the classes and forget to call base.OnLoad then the Load event will not fire and consequently Page_Load will not be called either.
Is your BasePage inhereting from System.Web.UI.Page ?