[TemplateContainer(typeof(MasterPage.Site1))] - c#

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)
}

Related

Weird Error on Visual Studio - "you could use the navigation bar to switch context"

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

asp.net c# codefile not working properly

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.

Cannot reference my web usercontrol (ascx) from code behind

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" %>

How to call a function from Masterpage into new form in c#

I've a Master Page which contains a DropDownList. I've a function for binding the list in the master and it works fine.
My problem is: How will I call that Master Page function from a form, which is not the child of the above master page
See article here.
Here is something nice that comes from the new compilation model in ASP.NET 2.0. Let’s say you add a custom property to a master page code-behind file like so:
partial class otcMaster : System.Web.UI.MasterPage
{
public string FooterText {
get { return Footer.Text; }
set { Footer.Text = value; }
}
}
You can get to the master page for a web form using the inherited Master property, which returns a MasterPage reference. To get to a property defined in otcMasterPage though, you might think you need to use a cast.
((otcMaster)Master).FooterText == "foo"
Casting to a derived type is just a part of life when using frameworks and statically typed languages, but there is a better way. Use the # MasterType directive in the ASPX.
<%# MasterType VirtualPath="~/otc.master" %>
Now when ASP.NET codegens the page, it puts the following inside a partial class definition. Notice the Shadows keyword (that would be the new keyword in semicolon land [yeah, I’m experimenting with alternative languages]).
public new otc Master {
get { return (otcMaster)base.Master; }
}
The result is a strongly typed Master Page. We don’t need a cast, we can go right to the Master.FooterText property. Another way to do this is to specify a TypeName in the #MasterType directive.
Provide a public method in your MasterPage, then you need to cast the ContentPage's Master property to the appropriate type:
public void DataBindDropDowns()
{
// ...
}
Then you can call it from your ContentPages in the following way(assuming your masterpage's type is called SiteMaster:
((SiteMaster)this.Page.Master).DataBindDropDowns();
Edit:
...which is not the child of the above master page
I assume that means it's no a ContentPage of that Master, am i right?
Then it's not possible to get a reference to the master except when:
The master's method is static, what is impossible in this use case since you want to bind controls on the master
You have a reference to a page which master is of that type, but again impossible since the current HTTP-Handler is another page which does not use this master
Note that the master page actually is a child of a ContentPage and will be merged with it. It's not possible to get a reference to an object that does not exist!
From MSDN:
Note that the master page becomes a part of the content page. In
effect, the master page acts in much the same way a user control acts
— as a child of the content page and as a container within that page.
You need to refer to MasterPage property, cast to your master page type and invoke your method.
((MyMasterPage)this.Master).MyBindingFunction();
Put this in your page code (where MyMasterPage is your master page object):
MyMasterPage masterPage = (MyMasterPage) this.Master;
masterPage.MyBindDropDownListFunction(); // Replace with your public function name
If you're making it very frequently, you can create a BasePage derived from System.Web.UI.Page, and use it as the bage page for your forms.
There you can add a property of the type of your master page, that will give you acces to all public members of the Master Page.
If you master page class is Site1, you could do something like this in your BasePage.
public class BasePage : System.Web.UI.Page
{
protected Site1 Site1Master
{
get { return Master as Site1; }
}
}
Then in the pages where you need to acces the methods of the master page replace:
public partial class DefaultPage : System.Web.UI.Page
with
public partial class DefaultPage : BasePage
Then you'll have the property Site1Master available in the pages, and you can use any of its public members like this:
Site1Master.MyBindingFunction(...);
You can also add any other desired functionality in your BasePage.
NOTE: If you want to make sure that the property isn't null in the pages, you can add a check to see if the page has the Site1 master, like this:
protected Site1 Site1Master
{
get
{
if (!(Master is Site1))
throw new Exception("This page doesn's have Site1 as master page");
return Master as Site1;
}
}
For accessing the members of a Master page there's a Master property exposed on Page Content.
First you've to specify the # MasterType directive :
<%# Page masterPageFile="~/MasterPage.master"%>
<%# MasterType virtualPath="~/MasterPage.master"%>
Then in the Master Page create a Public function and in your content Page you simply call
Master.MethodNameInMaster()
For better design use EventAggregator pattern. Create your custom event and handle it in Master Page.

problem with inherits and src attributes of page directive

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" %>

Categories

Resources