I am having a problem with the page directive. The code behind file is CodeBehind.cs within the namespace aspnetian.
when i specify the inherit and src attribute like...
<%# Page Language= "C#" src="~/CodeBehind.cs" inherits="aspnetian.CodeBehind.cs" %>
Parser Error Message: The base type 'aspnetian.CodeBehind.cs' does not exist in the source file '~/CodeBehind.cs'.
If I remove the namespace and specify the directive like...
<%# Page Language= "C#" src="~/CodeBehind.cs" inherits="CodeBehind.cs" %>
it gives this error:
class 'codeBehind' and namespace 'CodeBehind', declared in 'D:\AspPractice\WebApplication1\WebApplication1\codeBehind.aspx.designer.vb', conflict in namespace 'WebApplication1'. D:\AspPractice\WebApplication1
\WebApplication1\codeBehind.aspx.vb
Please tell me where I am mistaken and whats the correct way.
The inherits property should be describing the class the Page will use. I'm assuming the class you created looks like so:
public class CodeBehind : public Page
{
// blah...
}
If that is the case then you just need to drop the .cs from the end of the inherits property.
Try this:
<%# Page Language="C#" CodeFile="CodeBehind.cs" Inherits="aspnetian.CodeBehind" %>
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 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 have inherited someone elses code and was wondering if there is any reason I would need to keep both of these in the page directives? I have tried it removing classname and things still seem to work. Just wondering.
<%# Control Language="C#" AutoEventWireup="true" CodeFile="YourProgram.ascx.cs" Inherits="program.YourProgram" ClassName="program.YourProgram" %>
If your ascx file doesn't contain any code, the ClassName attribute is not needed, see http://blogs.msdn.com/b/thirusrinivasan/archive/2008/07/16/classname-vs-inherits.aspx.
However, Inherits and ClassName do different things. ClassName will set the name of the class generated out of the ascx file, while Inherit will make the generated class inherit from the class in the code behind:
http://msdn.microsoft.com/en-us/library/vstudio/d19c0t4b(v=vs.100).aspx
I'm trying to make my masterpage work with my content page, allowing the content page to access the master page controls. I get the error:
Parser Error Message: The 'mastertype'
directive must have exactly one
attribute: TypeName or VirtualPath
This is on the lines:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="viewProduct.aspx.cs" Inherits="AlphaPackSite.viewProduct"
MasterPageFile="MasterPages/Main.master"
title="Hi there!"
%>
<%# MasterType TypeName="Main" VirtualPath="MasterPages/Main.master" %>
My master page is:
namespace AlphaPackSite
{
public partial class Main : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
I'm a bit confused with namespaces and such so may have got them wrong, but all pages are in the same namespace now I beleive.
Update
When I turn it to:
<%# MasterType VirtualPath="MasterPages/Main.master" %>
I keep getting the error:
Compiler Error Message: CS0030: Cannot convert type 'AlphaPackSite.Main' to 'ASP.masterpages_main_master'
Source Error:
Line 147: public new ASP.masterpages_main_master Master {
Line 148: get {
Line 149: return ((ASP.masterpages_main_master)(base.Master));
Line 150: }
Line 151: }
Unless you want to keep a reference of your master page in the current page (in which this code is written), I'd suggest you remove the <%# MasterType VirtualPath="MasterPages/Main.master" %>
line.
This line provides a way to access your page's master page (such as when you've to change a label on the master page or the menu needs to add a few more items etc.). If the content of your master page does not require any changes/updates from your content page there's not need to use the MasterType tag. Because by using both MasterPageFile="MasterPages/Main.master and MasterType, you're confusing the compiler (even though the master page is same).
Update
If you have to keep the MasterType tag, then remove the MasterPageFile="MasterPages/Main.master attribute from the page directive.
As the error says, #MasterType expects only one parameter. Try just:
<%# MasterType VirtualPath="MasterPages/Main.master" %>
See http://msdn.microsoft.com/en-us/library/ms228274.aspx for more info.
(1) comment out MasterType directive and compile the web site
(2) uncomment the masterType directive and place it with either type name or virtual path, both will throw error
Reason for commenting and uncommenting:
Logically, when website fails to build, it will not have
AlphaPackSite.Main created and hence will throw error
but once you comment it out, and there are not other errors in the code, you will hopefully get the type in your bin!
So there are more chances to work with comment > compile > uncomment kind of things
reference for MasterType:
http://msdn.microsoft.com/en-us/library/ms228274.aspx
Try:
<%# MasterType TypeName="AlphaPackSite.Main" %>
I saw this question while searching for another answer and figured I would provide the answer quickly in case other people have this issue.
The problem is that you do not want to use a virtual path in the master name call. Instead you need to use TypeName and add in underscores for your path. Here is the correct tag based on your path:
<%# MasterType TypeName="AlphaPackSite_Main" %>
Given two assemblies:
project.web
project.lib
The project.web assembly references project.lib which contains some business logic. A simple class from project.lib:
public class Person
{
public string Name;
}
In project.web.Controllers:
Using project.lib.models;
public class PersonController : Controller
{
Person person = new Person();
public ActionResult Index()
{
return View(person);
}
}
This is the part where I have some questions. In a lot of sample projects I've seen the following in the View (in this case Index.aspx):
<% #Import Namespace="project.lib.models" %>
Allowing you to use the Model object like this:
<%= Model.Name %>
I haven't gotten that to work like the examples, I've had to do:
<%= (Model as Person).Name %>
or
<%
var person = (Person)Model;
Response.Write(person.Name);
%>
Why is this thus? What is the reason for this thusness? Comments? Suggestions? My class definition looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace project.lib.models
{
public class Person
{
public Int64 PersonID;
public string DisplayName;
public string RealName;
}
}
Could you please post your <%# Page %> directive? It should be something like:
<%# Page Language="C#"
[...]
Inherits="System.Web.Mvc.ViewPage<project.lib.models.Person>" %>
Have you tried creating a strongly typed code-behind file for this view instead?
You'd need to update the inherits property of the view to be the code-behind file:
<%# Page Language="C#"
CodeBehind="Index.aspx.cs" Inherits="project.web.Views.People.Index" %>
and then in the code-behind:
namespace project.web.Views.People.Index {
public partial class Index : System.Web.Mvc.ViewPage<project.lib.models.Person>
{}
}
I don't think it's an issue with resolving the System.Web.Mvc.ViewPage class as then the page wouldn't load (you'd get an exception along the lines that System.Web.Mvc.ViewPage cannot be found), and you wouldn't even be able to access the Model property.
Your view may not be strongly typed. Try the following:
Copy the View markup
Delete the View
Go to the controller action that drives the View
Right click within this method, select the Add View option.
Ensure that this is a Strongly typed view and that you properly identify the type of your intended model.
Paste your View markup into the newly created view.
This should work. It doesn't look like you're missing an assembly because you're able to cast the model correctly.
It should be noted that you can do the above without the pain of deleting/restoring a View. Each view has a declaration as its first line that is similar to the following:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<DomainModel.Person>" %>
The important section to understand is the Inherits attribute, which implies that this View extends the MVC ViewPage<DomainModel.Person> class. The Model property will be of type DomainModel.Person at runtime.
Also, there are issues with previous versions of ASP.NET MVC that effectively prevent strongly-typed views from working correctly. See Problem using Add View dialog in ASP.NET MVC RC1 for strongly typed view for more detail.
EDIT: After a bit of detective work by everyone who has posted here, it looks like Visual Studio was referencing older versions of the assembly containing the Person class. Closing the development environment down, clearing out all obj/bin folders, and restarting seems to have fixed this issue.
Update 1: Note that you can be pretty certain that it isn't an issue loading/getting to the types System.Web.Mvc.ViewPage or project.lib.models.Person. As you mentioned in your question, it does work when you use:
<% #Import Namespace="project.lib.models" %>
...
<%= (Model as Person).Name %>
Using System.Web.Mvc.ViewPage<project.lib.models.Person> on the page directive and <%= Model.Name %> must work. Some extra things I would try if that doesn't work:
Remove the import directive when doing so.
Check if you have that namespace added on the web.config.
While none of those 2 above should cause any trouble, if you don't get it to work with what is mentioned before that, it would be a very weird scenario. Also note that if you did add commonly used namespaces in the web.config you can use ViewPage<Person> on the page directive.
The Model.Name you refer to doesn't have to do with importing the namespace, you get it from the type you set in the page directive.
The ViewPage class has the property T Model, which is why you get access to the Name property (you are working with the type you specified).
to do that your view should be strongly typed to your model object in your case the Person class
all you need to make it work is adding refrence to the project.lib and adding updating the page derictive :
<%# Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<Project.lib.Models.Person>" %>