ASP.NET what is the meaning of AutoEventWireup and Inherits? - asp.net

Given the following statement,
<%# Page Language="C#" AutoEventWireup="true" CodeFile="XXX.aspx.cs" Inherits="XXX" %>
What is the meaning of AutoEventWireup?
What if the value of AutoEventWireup is equal to false
What is the meaning of XXX in the Inherits attribute?
I cannot find the definition of XXX in the auto-created file in ASP.NET 2008. Where is the XXX defined?
Thank you

AutoEventWireup = false means that your Page_Load event will not be automatically hooked to the page's Load event and so on for PreRender and the other page lifecycle events. It means in the constructor of your code-behind base class for the Page, you will have to manually do
Load += new ..EventHandler(Page_Load) etc
Inherits tells the page which class is the base class for the class that the runtime will generate when your application starts up. The auto-generated class will be in the ASP namespace and be put in the Temporary ASP.NET Files and will inherit from your class. This is how protected properties and event declarations in your code-behind can actually serve as handlers that are specified in the declarative .aspx markup
XXX is usually side-by-side right next to the aspx file and is the same name as the aspx file, unless it is Default, in which case that is a C# keyword, so sometimes it uses _default as the class name while the page itself is Default.aspx.
You should probably try to read some tutorials on ASP.NET page inheritance, here is an example, but you should search for more:
http://west-wind.com/weblog/posts/3016.aspx

Related

access master page from ascx control

I have a public property in a master page. I want to access this property from an ascx user control.
My master page is named master_public and it's in a namespace called "master".
So I tried writing:
dim m=ctype(page.master,master.master_public)
dim foobar=m.foobar
The intellisense says that master.master_public doesn't exist.
I tried removing the namespace from the master page. Still no luck.
The master page is declared "partial public class". But this name doesn't seem to be recognized.
Answers here Accessing Master page control in ascx file seem to imply that this should just ... work. Is there some attribute or setting or something I have to include to make master pages accessible as class types?
Add the following on top of your form (.aspx):
<%# MasterType TypeName="master.master_public" %>
The above directive will expose public members of the masterpage to the form. To access your property from the form, simply reference as below:
Me.Master.YourProperty
Therefore, in order to access the masterpage public property from user controls added to the form, just cast the master page object :
CType(Me.Page.Master, master.master_public).YourProperty

Master page inheritence dilemma

Bit of a confusion for me here. This is ASP.NET 4.0. Looking at the markup, it appears that Default.aspx inherits from Site.Master page. But looking at the class definition of Default.aspx (which is named _Default), it inherits from Page class and not SiteMaster.
Now I need to share a few functions across multiple inherited pages and was looking to add them to SiteMaster class so that they would be available in all inherited pages. Can I use SiteMaster class for my purpose, or should I add an independent module to my project and add all my functions to that?
Figured it out. For anyone else looking for a solution, all you need to do is to add the following directive to your markup:
<%# MasterType VirtualPath="~/Site.Master" %>
You can add this line to the top of ASPX file, just under the <%# Page ... %> line. Once added, simply rebuild your project. Now all the public members of your master page class will become available through the strongly-typed member named Master. For example, you can now use the following code to access a public function called MyFunc() defined in the master page class:
this.Master.MyFunc(); //C#
Me.Master.MyFunc() 'VB.NET
MasterType directive actually adds a shadowing member named Master that is strongly-typed as SiteMaster class (or whatever master page class you have specified) when spitting the code-behind class. This new member hides the base class member (whose type is the generic MasterPage class) and thus allows you to access your master page members directly. The property is defined in the .aspx.Designer.cs/vb file like this:
Public Shadows ReadOnly Property Master() As YourProjectNamespace.SiteMaster
Get
Return CType(MyBase.Master, TagTrakkerOnline2.SiteMaster)
End Get
End Property

ASP.NET User Control instance is null when referenced on Page_Load on page code behind

I have a user control that I have written and have added to an ASP.NET page, and functions just fine. However, I am trying to reference a property in the that custom control from code behind, on Page_Load, but cannot, because the variable, which is accessible, for the instance is null.
Is this normal for user controls and Page_Load? And if so, how can I make a reference to the control's instance in order to access its public properties? This is something I need to do before the page is rendered, in order to initialize some variables.
I had the same issue, and it turned out that I was registering my custom control incorrectly.
Correct Definition:
<%# Register Src="PeriodControl.ascx" TagName="PeriodControl" TagPrefix="ucs" %>
Incorrect Definition:
<%# Register TagPrefix="ucs" Namespace="MyWebsite" Assembly="MyWebsite" %>
The only difference was to reference the ascx file directly instead of the control in the assembly. Go figure!?
You can probably access your user control from the Page_PreRender event.
You can find more documentation about the page life cycle in asp.net here.

Mapping a .NET Class to a custom page extension

What I would like to have is a mapping between a custom file extension to a class that is not System.Web.UI.Page but a class of mine that inherits from System.Web.UI.Page.
i.e.:
*.aspx -> System.Web.UI.Page
*.my -> My.Package.MyClass (inherits from System.Web.UI.Page)
I know I can map any extension to be treated like .aspx but I can't find the way to do what I have in mind.
Thanks
d
You need a ASP.Net HTTPHandler for this.
I know you mentioned that you already inherit from System.Web.UI.Page but also look into the lighter weight ASHX Handler
A custom page handler, but also any ASP.NET Page can inherit from your custom base class, so you wouldn't necessarily need to do a handler. You could just have the code-behind pages all inherit from My.Package.MyClass instead.
I think I was trying to build a castle when I just needed a hut.
When I'm in case of a .aspx page without a separate code file I just have to add the Inherits directive, making it pointing to my custom class, like this:
<%# Page Language="C#" Inherits="MyPackage.MyClass" %>

Dynamically adding user controls registered in web.config

I'm working on a project that has all its user controls registered in its web.config file (which seems very clean and tidy)
So far so good (here comes the problem) however I'm trying to dynamically create and add user controls to a page. These user controls fire events that need handling.
Ordinarily that wouldn't be a problem:
You just register the control in the page, load the control, cast it to the correct type, and assign the event handlers, add it to the page, sit back and let the magic happen, easy peasy.
But I can't reference the control's type when the control is registered in the web.config, which means no cast, which means no event handling!
Weirdly you can reference the type if you add the usercontrol to the page at design time!
There must be a way round this (without having to register the control on the page, or add a control at design time), what on earth am I missing?
It's been a while, but I think I've seen this type of behavior in ASP.NET when a project is a Web Site and not the Web Application. As far as I remember, the Web Site compiles each page into its own assembly and with no common name space and regardless of config requires the <%# Register %> directive. If you don't, you get the exact error of missing an assembly reference.
I would have to test to be sure...
By saying : "you can reference the type if you add the usercontrol to the page at design time"
Do you mean it adds an <%# Register %> Directive at the top of the page ?
Or maybe, it adds a using / Imports (depending on you using c# / vb.net) clause in your source document ?
Because, to be able to cast to your control type, you normally need to import the namespace in the codebind. Maybe this is just what is missing.
The <controls> section in web.config and the <%# Register %> directive are the same thing (with the small exception that entries in web.config apply to the whole application). They allow you to add design-time controls to a web form.
If you want to add controls to a page dynamically, use the LoadControl function to get an instance of your control. Given a control with a class name of "Header", the following will load a control, set a property, and add the control to the form named, "form1":
Dim head As Header = LoadControl("~/Controls/Header.ascx")
head.Text = "Some text..."
Me.form1.Controls.Add(head)

Resources