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

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.

Related

Double Clicking Asp button in Designer mode creates function in unwanted aspx.cs fle

On double clicking the asp:Button in Design mode, a button click function is generated in .aspx.cs file of some other .aspx file. How do I make it such that on double clicking the button, it opens in the same page's cs file?
Note: I am using Visual Studio 2022 (free)
Example :-
I have 2 web pages:-
web.aspx (web.aspx.cs)
web1.aspx (web1.aspx.cs)
Now if I double click on a button in design mode of web.aspx file, then a button_click function is being created in web1.aspx.cs file, but Onclick event is being added to the button in web.aspx.
How do I make it such that on double clicking, the function is created in web.aspx.cs itself ?
Most likly the issue is that new page was NOT created by Visual Studio, but you made a copy of the page.
Check the topmost line in the markup page.
so, say the page is called MyUpLoadTest2.aspx
(the "2" kind of gives away the fact that I made a copy of this page).
Note this part:
<%# Page Language="C#" AutoEventWireup="true"
CodeBehind="MyUpLoadTest2.aspx.cs"
Inherits="CSharpWebApp.MyUpLoadTest2" %>
Check both codeBehind, and make sure it matches the page name (MyUpLoadTest2).
And also check the Inherits in above - again make sure it matches the code behind.
Also, view the code page, and make sure the class name for the page matches the page name.
eg this:
public partial class MyUpLoadTest2 : System.Web.UI.Page
So, make sure all 3 match.
if the page directive in the markup does not match the correct name, or is pointing to a different code file, then you see the effect you note - the code will be created in the wrong page.
Another issue/thing to avoid?
Don't name the page the SAME as any class you have - including built in ones, as that can say mess things up.
So, for example, you might try to call a page GridView, but there is a gridview control, and thus you don't want your page to have the same name, since then the code behind page class will wind up with the same name as that existing class (built in ones, or even control names).
So, check your page directive.
In many cases, you are better off to create a new blank page, and then cut + paste in the markup between the "form" tags, and leave the rest of the page alone.

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?

how to get the code file of another aspx page attached to my aspx page

My problem is to make a code behind file of an aspx i.e already existing 1.aspx pages aspx.cs
to be available to another page aspx with out replicating any of the code in code behind.
i.e 1.aspx --> code file is 1.aspx.cs.
now 2.aspx --> code file is 1.aspx.cs
under the condition that the controls used in both aspx pages have identical Ids
Honestly, if you have such requirement, you should better encapsulate common behavior either in a base class that can be inherited by the two page's dedicated class, or create utility/business class that contains the code.
You should also consider using User Controls. This can help you to create reusable visual component in your application.

Add html layout to a blank aspx page

Want to create dynamic html layout without any asp controls. Actually I want to leave on aspx page only the first line
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Kletka._Default" %>
and the generate full html layout on codebehind. Advise pls how to implement this.
One way is this:
protected void Page_Load(object sender, EventArgs e)
{
string coolHTML = #"<div class=""someclass"">... and other cool content</div>";
Response.Write(coolHTML);
}
With that said. This is a terrible idea. Constructing HTML dynamically on code behind is a nightmare to maintain, it doesn't perform as best as it can and you lose many other features that asp.net offers, which are the main reason to use ASP.NET in the first place.
What you can do is create User controls for specific things (footer, header, left panel, etc) and define a layout for them in markup; then on Code behind, you can add them to specific place holders in the page, depending on some business conditions.
Assuming you have a master page (or at least some content place holders in the page) as so:
<asp:ContentPlaceHolder id="footer" runat="Server" />
On code behind you can do:
footer.Controls.Add(new FooterControl());
Update OP just mentioned in the comments that he doesn't like asp.net controls...
You don't have to use ASP.NET controls, you can use regular HTML controls and set their runat="server" attribute if you need to be able to manipulate their properties on server-side. For example:
<div id="mydiv" runat="server" > some content </div>
On Code behind:
myDiv.Attributes.Add("class","css_class");
myDiv.Attributes.Add("onclick","callJavascriptFunction();");
// and so on.
It's okay to do this sort of thing occasionally under very specific circumstances but I'd avoid this sort of code because is difficult to maintain. Imagine you need to add another class to the myDiv element, for example. Now, you'd have to change you code as opposed to just changing your markup...
So...you want to use ASP.NET web forms without using the built in controls like GridView and so on, at all?
You can write your html and use protected properties?
<%= SomeWhereText %?
or to have the FULL html layout in the code behind make a property
protected string MyEntirePage {get;set;}
and build the string in the code behind
MyEntirePage="<h1>Hello</h1><p>body here</p><p>the end</p>";
and write it out in the aspx page via
<%=MyEntirePage%>
Re: "I've got your point, but I really don't like asp.net controls. I'd prefer to use html controls and customize them with js"
Install NancyFx or maybe the old (but still great) WCF Web Api and use something like KnockoutJs, jQuery or Backbone to perform ajax calls for the dynamic content = no asp.net web forms at all. Yay.
You would need to dynamically add the controls in the Page_Init event. So you need a container to hold your HTML, so you should start by adding a Panel to the Page, then the page's controls would get added to the Panel.
Panel pnl = new Panel();
Page.Controls.Add(pnl);
TextBox txt = new TextBox();
pnl.Controls.Add(txt);
etc....

C# ASP.NET Custom Control not showing up

I'm trying to construct a custom control for ASP.NET
I started by creating a Web Application in VS2010 and creating a new .ascx page. The page is called "TestBox" and it's just a single TextBox control with "This is a test" as the text.
I built the project and then included the DLL in another website in order to make sure I would be able to move controls. Based on a tutorial I found here I added the following line of code to the top of the page:
<%# Register TagPrefix="TestControl" Namespace="TestControl" Assembly="TestControl" %>
Then I added this to the page itself:
<TestControl:TestBox ID="TestBox1" runat="server" />
The code compiles and the page loads without throwing up any errors, but when it loads it's completely blank. By introducing a deliberate runtime error, I determined that the TextBox is definitely being loaded, but the control itself still isn't showing up.
Am I missing something?
Code for the TestControl:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="TestBox.ascx.cs" Inherits="TestControl.TestBox" %>
<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged">This is a test</asp:TextBox>
I haven't touched the Designer code or the .cs code in any way.
EDIT: Figured it out. I had declared a namespace for the .CS file but not the .ASPX file itself.
The answer was that I had to add a namespace to the ASPX file itself and not just the underlying code file. I forgot to add Class="TestControl.TestBox" to the page declaration.

Categories

Resources