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
Related
TL;DR
Is it possible to replace the Inherits="..." of a UserControl using a class defined in a code-block, rather than the class in the compiled code-behind?
I have an ASP.NET web application that I quickly need to patch, but I am only allowed to update the .aspx and .ascx files... it is not possible for me to re-compile and release .dll files (due to the change management processes the client puts on us).
In the application I have a UserControl that is set to Inherit from the code-behind class for the file... but there is a bug in that class.
What I want to do is re-create the entire class within a <script runat="server"></script> block in the .ascx file with the appropriate bugfix.
I've tried the following, but I get...
Could not load type 'MyCtrlStatic'.
<%# Control Language="vb" AutoEventWireup="false" Inherits="MyCtrlStatic" %>
<script runat="server">
Public Class MyCtrlStatic
Inherits UserControl
Public Function DisplayValue() As String
Return "Hello World"
End Function
End Class
</script>
<div><%=DisplayValue()%></div>
The control in question is using a lot of different properties and methods from the code-behind class, and the above is a massive simplification of the issue.
Is this possible to achieve? Otherwise I'm looking at re-building and going through an entire change management procedure.
The answer was staring me in the face, and I couldn't see it until just a few minutes ago...
Instead of trying to declare a new Class and inherit from it, the answer is to inherit directly from UserControl and then have the code directly in the code-block.
<%# Control Language="vb" AutoEventWireup="false" Inherits="UserControl" %>
<script runat="server">
Public Function DisplayValue() As String
Return "Hello World"
End Function
</script>
<div><%=DisplayValue()%></div>
In addition the above, I also had to add a few Import command, such as...
<%# Import Namespace="System.Collections.Generic" %>
But now I have a patched .ascx file that doesn't require the rebuilding of the DLL to work
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 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" %>
after weeks of having this issue I finally decided to ask for a solution to the following problem:
In the .aspx page you can set
<%# MasterType VirtualPath="~/Mastername.master" %>
This results in an auto generated property in the .aspx.designer
public new Mastername Master {
get {
return ((Masternamee)(base.Master));
}
}
Works perfectly fine. But if I do changes in the .aspx file, the property will be new auto generated and it looks like the following:
public new NAMESPACE1.Mastername Master {
get {
return ((NAMESPACE1.Mastername)(base.Master));
}
}
Compiling will not be possible afterwards, because the class for the MasterPage cannot be resolved at the given namespace.
The masterpage has NAMESPACE1 as namespace.
Every contentpage has the same NAMESPACE1.
The autogenerated property tries to look for the masterpage class in NAMESPACE1.NAMESPACE1 which will fail, due to it does not exist. Of course I can remove the first NAMESPACE1. to make the app compilable again, but it just sucks to do this nearly every time I make changes in the .aspx file.
Is there a way to avoid this problem? The only way I can think of, is to ignore the auto generated property and make a explicit cast everytime I want have access to the masterpage.
Edit: I'm using Visual Studio 2008 Professional SP1.
For some reason the designer believes that the master page is defined in namespace NAMESPACE1, so look at the master page definition (and code behind) to check its namespace has not been modified (possibly accidentally).
If there is nothing obvious, a search in all files (*.cs, *.aspx, *.master, ...) for NAMESPACE1 may be needed.
(This is where using a VCS would help --- you could check the history of changes.)
Actually it's more a designer "feature". ;-)
The Master name used in your designer file will be pulled from your .Master file's Inherits property. So change how you qualify the Inherits attribute, and that will change the class name used when the designer file is created.
I found a solution that works. I won't use the autogenerated property in the designerfile. I'll write my own wrapper property that I do implement in every contentpage.
I had this same problem when I added <%# MasterType VirtualPath="~/TestMaster.Master" %> to my aspx page in SOURCE view. For some reason, the page never created correctly and kept giving me invalid namespace errors until I actually changed to DESIGN view and resized a control and finally the error went away. Somewhere it was using some cached data (even a Build/Clean Solution didn't clear it out) and until the designer recreates the page, it generates that error.
Change
<%# MasterType VirtualPath="~/Mastername.master" %>
to
<%# MasterType TypeName="Mastername" %>
this will work perfectly