Cannot reference my web usercontrol (ascx) from code behind - c#

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

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

Add two aspx pages for single aspx.cs

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?

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.

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

ASP.net c# Masterpage problem

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

Categories

Resources