Editing Aspx Website Template with C# - c#

I have a template for a website, and I want to edit the aspx files with C#,
thats means that I want each of the aspx files have a code behind file, which is .aspx.cs file for each .aspx exist file.
I opened a new ASP.NET AJAX Website Template and copied the .aspx files, the webconfig and the css to the new website I created.
when I add control and double-click on it in order to create a .aspx.cs file for this page,
it brings me to the source code.
I've added this line as the first line of my aspx file in order to create a .aspx.cs file:
<%# Page Language="C#" AutoEventWireup="true"
CodeFile="Login.aspx.cs" Inherits="Login" %>
but still it dont let me create an aspx.cs file. Does someone know how can I do that?

Right click the folder your ASPX page is in (or the root project if it's in the root folder) and pick "Add New Item..."
Choose "Class", name it Login.aspx.cs, and click "Add"
If you're using a Website Project, it will ask you if you wanted to put the code file in app_code. Answer no.
Change the class declaration to match the Inherits property in your page directive, be a partial, and inherit from WebForms' Page base class:
public partial class Login : System.Web.UI.Page
{
}

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.

Call a function from MasterPage which is located in different folder

My website hierarchy looks like this-
What I want to do is to call a function which is in Login.master.cs file into Default.aspx.cs.
If the Default.aspx was in the same Login folder the master page would have been called by
var master = Master as Login_Master;
master.myFunction();
but the above code doesn't work if the master page is in another folder.
How could I call the function?
Thank you.

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?

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

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