How to add code behind manually on asp.net - asp.net

I have a website on the IIS but it has only the aspx file. Now I need to add the code behind for some pages. How do I go about this?
I've been trying to add the attribute "codebehind" and "autoeventwireup" on the top of the aspx file but no luck (the page_load event is not being called). Also, if I double click on the button from the design view in Visual Studio, it creates the javascript handle (not the server code).

In your page directive on the aspx page (<%# Page...), you need two pieces:
1) CodeFile="Default.aspx.vb"
2) Inherits="_Default"
In your code-behind file, you need two things:
1) The file name has to be whatever you specified above (CodeFile = "...)
2) the class name has to be whatever you specified above (Inherits="_...)
For example, if your aspx page was named "default.aspx" then your page direction in the aspx page would look similar to this:
<%# Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
And your code-behind would look similar to this:
Partial Class _Default
Inherits System.Web.UI.Page
End Class

you want the #page directive and the class attribute. MSDN docs here

Related

What happens if the Inherits attribute is removed from the #Page directive?

I don't know what happened with my project, but since I reinstalled my pc with Windows 8, the website doesn't build...
My #Page directives look summing like this:
<%# Page Title="Register" Language="vb" MasterPageFile="~/Site.Master" AutoEventWireup="false"
CodeBehind="Register.aspx.vb" Inherits="_Register" %>
The codebehind for this page looks like this:
Partial Class _Register
Inherits MobilePage
' subs go here (don't think they're important for the question)
End Class
With this set up this way, I'm getting the following error:
Could not load type '_Register'.
Removing the Inherits attribute fixes this error, but what else does it do in terms of the page's functionality?

asp.net mvc render a webform as a partial in a razor view

I'm trying to use the asp.net webforms sitemap and menu control to build my asp.net mvc's sites navigation.
There are many tutorials about embedding razor views into webforms, but I want to go the otherway around. Embed a webform into a razor view.
Doing something like this looks eemi promising:
#Html.Partial("~/Views/Shared/test/menu.aspx")
Which presents an error:
Parser Error Message: 'System.Web.Mvc.ViewUserControl' is not allowed here because it does not extend class 'System.Web.Mvc.ViewPage'.
Is there a value for Inherits that would allow this to work?
Oh Duh, read the error message...
I changed the first line of the webform from this:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="menu.aspx.cs" Inherits="System.Web.Mvc.ViewUserControl" %>
to this:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="menu.aspx.cs" Inherits="System.Web.Mvc.ViewPage" %>
Then I ran into another issue about System.Web.Mvc.ViewPage being defined twice, so I deleted the *.designer.cs file and now its working.

Unable To See Master Pages While Adding A Page In The Project

i am trying to add a .aspx page in the project , but while adding the .aspx page i am not getting the option to choose existing master pages.
Don't know why it is not visible; if you cannot set it, simply add it later manually to the #Page directive of your page.
It's simple:
MasterPageFile="~/Path/To/My/MasterPage.master"
E.g. on top of your ASPX page, write something like:
<%# Page
Culture="auto"
UICulture="auto"
MaintainScrollPositionOnPostback="true"
Title="The Title of your Page"
Language="C#"
MasterPageFile="~/Path/To/My/MasterPage.master"
AutoEventWireup="true"
CodeFile="MyPage.aspx.cs"
Inherits="MyPage"
%>
In addition, if you want to access the derived type of your master page, use the #MasterType directive:
<%# MasterType VirtualPath="~/Path/To/My/MasterPage.master" %>
Could be a bug with VS, try save the project with your master page then close and reopen VS.
If not as Uwe Keim mentioned above it is really easy to add the master page manually.

Dynamically access master pages properties

We can change masterpagefile for particular page dynamically.but how can i access their properies which are changing as per the master pages.
what code will give me their properties.
Thanks
You can add a public property to the code behind of your master page, like so:
public string MyMasterPageProperty {
return "my stuff";
}
Then, on the content page, you can use this code to access your new property.
Page.Master.MyMasterPageProperty;
If you don't want to add the MasterType directive on the aspx page, you can always explicitly cast the Page.Master to the type of your master page, so that it looks like this:
((MyMasterPageClass)Page.Master).MyMasterPageProperty;
You can read more info here:
https://web.archive.org/web/20210513005959/https://www.4guysfromrolla.com/articles/013107-1.aspx
You'll need the MasterType directive on the aspx page:
<%# MasterType VirtualPath="~/Site1.Master" %>
Then on your aspx.cs page, you can call the properties of the master:
Master.Property1 = "whatever you wanna do";
If you need more instructions:
http://dotnet.dzone.com/news/back-basics-%E2%80%93-using-mastertype

Creating a Template Item from a web form and auto generating the Inherits page directive

OK, so I'm trying to create a template item from a web form.
I have referenced this article: http://www.west-wind.com/weblog/posts/740022.aspx
and it exports the template and installs it fine.
When you try and use the template item within a project though it leaves the inherits="_Default" in the page directive like shown below:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="FormTemplateDev.aspx.cs" Inherits="_Default" %>
So it thinks all the new template items use the original form name pre-template generation. This is an issue in the code behind too as shown here:
public partial class _Default : System.Web.UI.Page
I have seen example of passing paramater strings through but havent managed to get anything to compile. I found something like below:
public partial class $safeitemname$ : System.Web.UI.Page
Does anyone have any suggestions?
This was the link to the list of available template parameters i found:
Project Template Parameters
OK so It is as simple as this:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="FormTemplateDev.aspx.cs" Inherits="$safeitemname$" %>
and...
public partial class $safeitemname$ : System.Web.UI.Page
It threw me because the page won't compile and the page turns red with rage! This means you will have to put these $slightlystrangeparameters$ in once you have got your template item completed and built. Add the parameters in and export it as a template.
Hope this helps someone else...

Resources