Add two aspx pages for single aspx.cs - c#

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?

Related

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.

Make ASP.NET application using Notepad

I have made an ASP.NET application using Notepad++. For this exercise I do not want to use Visual Studio, or any other tool. I want to understand the process.
I have created my website, and it is up and running fine, and all working well.
Now I want to add some C# code behind the pages, both for the master page and for individual pages.
So far, I have a file called Home.aspx, and I want to add a C# file to this.
I have created a file called Home.aspx.cs. Below is the full content of the file:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("LOAD");
Response.End();
}
But when the page loads, this file is not loading. Obviously I am missing something, but I am not sure what. Possibly a reference in my web.config or some other folder, or language reference to tell the page this is C#, or something to tell Page_Load to actually run?
Also, I want to do the same thing for my master page, which is currently called masterPage.master.
So would I make a file called masterPage.master.cs, or is it a totally different way, or can this even be done?
All references to this problem explain how to do this in Visual Studio, which I do not want to use.
You can in fact create an ASP.NET WebForms page without compiling .cs files explicitly.
Home.aspx
<%# Page Src="Home.aspx.cs" Inherits="HomePage" AutoEventWireup="True" %>
Notice that the # Page directive uses the Src attribute instead of the usual CodeBehind attribute.
(Instead of Src, you can alternatively use the CodeFile attribute and mark the code-behind class below partial.)
Home.aspx.cs
using System;
using System.Web.UI;
public class HomePage : Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("LOAD");
Response.End();
}
}
masterPage.master
<%# Master Src="masterPage.master.cs" Inherits="MasterPage" AutoEventWireup="True" %>
Same thing, except that you use the # Master directive instead of # Page.
(Again, instead of Src, you can alternatively use the CodeFile attribute and mark the code-behind class below partial.)
masterPage.master.cs
public class MasterPage : System.Web.UI.MasterPage
{
}
(I named the code-behind class MasterPage to match your file name, but to avoid confusion with the built-in ASP.NET MasterPage base class, you may want to choose a different name.)
By adding a CodeFile link to the page as follows:
<%# Page Language="C#" MasterPageFile="~/MasterPage/MasterPage.master" CodeFile="Home.aspx.cs" Inherits="Home" Title="Content Page"%>
and ensuring an inhereits tag is present, it is not necessary to compile the code.
So this is the correct answer
The .aspx, .js and .html files in your asp.net app does not need any compilation, but C# is compiled language, so every .cs file needs to be compiled.
You can use some compiler for that purpose or VS command prompt.
Look at the links as well:
https://msdn.microsoft.com/en-us/library/ms229859%28v=vs.110%29.aspx
https://msdn.microsoft.com/en-us/library/78f4aasd.aspx
https://kencenerelli.wordpress.com/2014/03/08/using-notepad-to-write-c-code/
It needs to be compiled in order to run.
It's possible to call msbuild.exe on your solution from the command line.
https://msdn.microsoft.com/en-us/library/ms164311.aspx

Is there an equivalent to "partial class" for aspx pages?

I have a very very very long page and for making the html easier to read, i'd like to split the pages in several sub-pages.
To manage my controls on the page, I already split my code-behind page in several files.
I'd like to do the same with my aspx. How would I call those sub-page and how would I start them? (like, no <%# Page [...]...>).
So far, I tried <!-- #Include virtual="~/path/page.aspx" --> with an empty aspx page (only 'test' in the aspx. no code behind).
It works, but then, VS2010 throw me a bunch of errors (Too many characters in character literal. )
edit
I'd rather avoid using user controls :
I already use plenty of those. In this case, there's no need for reusability. Those controls would also mainly only contains other user controls. And I don't want to diverge too much from the layout of all my other pages (which are shorter). Most page have 3-4 controls on them but this one has like 50. I'd just like to split it in multiple html page.
I was close to the answer. It seems the error thrown by VS2010 on the html went away after building the project. Still, I went back to having code-behind files for my sub-page, although VS sorta get confused about all the files.
Here's how I ended up splitting my page :
Parent aspx file :
<!-- #Include virtual="~/Page/subPage.aspx" -->
parent aspx.cs file :
public partial class myPage
Subpage aspx file :
Plain html (no #Page, #Imports, etc.), user controls...
<div style="margin:20px 0px;">
<asp:Panel runat="server" ID="table" />
</div>
<CUSTOMCTRL runat="server" />
Subpage aspx.cs file :
public partial class myPage
Subpage aspx.designers.cs file :
public partial class myPage
The only issue with this is the subpage designers : You need to manage them by hand.

access function in aspx page to master page

I have a.master and b.aspx .
i have some functions in my aspx page.
how to access that functions in a.master page.
thank you
Let's say, you want to call Foo from b.aspx from a.master. So first thing is that you have make the method internal (or public) and then you can use code such as below in master page is call that method.
var page = (b)this.Page;
page.Foo();
Note that b will be the code behind class name in b.aspx. Note that above code will fail if you use another page c.aspx and use the same master a with it. Generally, I will say that invoking page specific functions from master does not make sense unless functions are present in some base page class and in such case you should be casting to that base page class.
Edit: More elaborate example as requested by Asif:
Consider your content page b.aspx such as
<%# Page Language="C#" MasterPageFile="a.Master" Title="Page B" AutoEventWireup="true"
CodeBehind="b.aspx.cs" Inherits="YourProject.b" %>
And in code behind file (b.aspx.cs), you have a method Foo such as
namespace YourProject
{
public partial class b : System.Web.UI.Page
{
void Foo(string someParameter)
{
Label1.Text = someParameter
}
...
}
}
Now in code behind (a.master.cs) of a.master page
namespace YourProject
{
public partial class a : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
b contentPage = (b)this.Page;
contentPage.Foo("Hello");
}
....
}
}
Of course you can make the method in b.aspx be public to call it in a.master. However I suggest you consider your design carefully. Because it's really weird just like that you call a method of a child class from its parent class (even though it's theoretically possible). Before your modification, ask yourself:
Is it necessary to call this method in the master page? If yes, do I have a better place to put the method?
As others have said, it's possible to do this. However, it's an odd way of doing things. You are probably going to be better off doing whatever you want to do in a different way. The whole idea of a master page is that it "wraps" many kinds of content pages. What if you content page doesn't have the function you want to call?
You could make sure all your content pages have the function, but then why not just put it in the master page?
Perhaps if you descired what you wanted to do a little better, we could advies you on a better way to handle things.
To access, either:
Make that method a static method.
Move your code in App_Code folder.
Move your code out of your web project, into some generic assembly and use that as a reference.

Can't access master page from user control

My master page code looks something like this:
namespace Recipes
{
public partial class MasterPage : System.Web.UI.MasterPage
{
...
public void UpdateUserLogin()
{
NicknameLbl.Text = ((Recipes.BasePage)Page).CurrentUser.Nickname;
}
...
}
}
I want to call the UpdateUserLogin() method from a user control, something like this:
((Recipes.MasterPage)this.Page.Master).UpdateUserLogin();
But for some reason the compiler doesn't know Recipes.MasterPage (are you missing an assembly blablabla).
Sorry I can't show the exact error message, it's in French.
Maybe the problem is that I added the Recipes namespace around MasterPage manually, it wasn't added by VS.
By the way I'm using VS Web Developer Express 2008.
Do you have any idea how I can make this call work?
Both the MasterPage and the UserControl are child controls of the page they are used by. Your UserControl could potentially be used in a page that doesn't use your MasterPage, and so calling UpdateUserLogin() would not be valid.
You can check it like this, however, and make your call conditionally:
if (Page.Master is MasterPage)
{
((MasterPage)Page.Master).UpdateUserLogin();
}
UPDATE
It seems you were already aware of that, sorry. Your question is about the reference not working. What is the namespace of your UserControl?
I would recommend data binding the NicknameLbl to the CurrentUser.Nickname property. Then the NicknameLbl text will get updated automatically if the property changes.
include a MasterType directive at the top of the Content page ASPX file:
'<%# MasterType virtualpath="~/DetailsMaster.master" %>'
include a public method in the Master page
public void UpdateUserLogin(string value)
{
NicknameLbl.Text = value;
}
access the method from the Content page using the Master syntax:
Master.UpdateUserLogin(Some Text");
http://www.codeproject.com/KB/aspnet/Master_and_Contents.aspx
If your project is a Web Site Project (instead of a Web Application Project), then you do not have a project namespace. All code that is referenced from aspx.cs or master.cs files needs to be stored inside the App_Code directory, as the ASP.Net compiler will create several assemblies instead of just 1, and its not predictable which assembly will contain which aspx code.
Update after 1st comment:
The .ascx.cs and .aspx.cs stay where VS puts them. But it you want to reference classes etc, this needs to be placed inside App_Code, e.g. your Recipes.MasterPage or Recipes.BasePage objects.

Categories

Resources