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.
<%@ 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
.)
using System;
using System.Web.UI;
public class HomePage : Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("LOAD");
Response.End();
}
}
<%@ 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
.)
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With