Page_Load not loaded after inheriting my own Page - c#

I am trying to create BasePage Web.UI.Page that is inherited by my main page.
But when i create public class mypage : BasePage method Page_Load of this class is not loaded in page live cycle.
BasePage does not contain any Page_Load.
has anybody got a clue where can be the problem?
thanx

1) Add class to your project
public class BasePage : System.Web.UI.Page
{
}
2) Assuming that you want your Default.aspx page to inherit from BasePage class, modify Default.aspx.cs file in the following way:
public partial class _Default : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
System.Diagnostics.Debugger.Break();
}
}
3) Do not change line <%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %> in your Default.aspx file.
4) Press F5 to start debugging. You will be asked to enable debugging in your web.config file. Press OK. You'll be stoped inside of Page_Load method, which means success.

I suspect your problem is that the event isn't actually called Page_OnLoad in the class, it's OnLoad. Here's how you want your class to look:
public class MyPage : Page
{
protected override void OnLoad(EventArgs e)
{
// Your code
base.OnLoad(e);
}
}
More detailed tutorial here.

Make sure the AutoEventWireup attribute is set to true. More information: http://msdn.microsoft.com/en-us/library/59t350k3%28VS.71%29.aspx

If you override OnLoad on some of the classes and forget to call base.OnLoad then the Load event will not fire and consequently Page_Load will not be called either.

Is your BasePage inhereting from System.Web.UI.Page ?

Related

[TemplateContainer(typeof(MasterPage.Site1))]

I have this error. How can I solve it?
CS0426: The type name 'Site1' does not exist in the type 'MasterPage'
Here is my masterpage code:
The other answer will most likely be the cause. However, I have also run into this error if you name your project a reserved name.
For example, if you name your project "Login" or "UpdatePanel" you will also get this error.
The fix is to name your project something else. For example "CompanyLogin" or "CompanyPanel".
You need to set your inherits attribute on markup of master page with your fully qualified class name . Saying that open "Site1.master.cs" and you will see your class name which inherits from MasterPage .if class is part of a namespace then you should set youtr inherits attribute with [Namespace].[ClassName]
For example in my sample project. My master page has following directive on top
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="SiteMaster" %>
And My Codebehind class (Site.master.cs) is : see SiteMaster is the class my master page inherits from. Asp.Net Form engine compiles markup into a class which derives from a base class i behind the scenes
public partial class SiteMaster : MasterPage
{
private const string AntiXsrfTokenKey = "__AntiXsrfToken";
private const string AntiXsrfUserNameKey = "__AntiXsrfUserName";
private string _antiXsrfTokenValue;
protected void Page_Init(object sender, EventArgs e)
}

Weird Error on Visual Studio - "you could use the navigation bar to switch context"

I have been trying to link a a web form to be a able to access a variable in the master page. I did this before it worked. But now when I do it I get an error.
The code in Site.Master
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="Site" %>
The code in Site.Master.cs
public partial class Site : System.Web.UI.MasterPage
{
public string hi = "";
protected void Page_Load(object sender, EventArgs e)
{
}
}
The code in WebForm1.aspx:
<%# Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="WebForm1.aspx.cs" Inherits="WebForm1" %>
<%# MasterType VirtualPath="~/Site.master" %>
The Code in WebForm1.aspx.cs
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Master.hi="new"
}
}
When I hover over the hi, I get this message
Error in Visual Studio - "You can use navigation bar to switch contexts
Here is another image
Screenshot
If you guys could help me it will really be great
I had this same problem in an MVC application so the WebForm-CodeFile-CodeBehind suggestion didn't really apply.
However, I realized the new classes that I created had their "Build Action" set to "Content". Once I changed this to "Compile", everything started working again.
In your WebForm1.aspx, try changing CodeFile to CodeBehind. That's what worked for me.
Try adding manual the ID of the form into the designer "yourname".aspx.designer.cs, in your case I guess it should be WebForm1.aspx.designer.cs.
You'll see a reference line for every id you have. You need to add a line like this -> "protected global::System.Web.UI.HtmlControls.HtmlForm hi" if you added the form like normal html
or -> "protected global::System.Web.UI.FormView hi" if you added as an aspx element

asp.net c# codefile not working properly

I am trying to add a codefile to my asp.net masterpage. I had it working properly on an individual page, but cannot do so on my master page.
In my aspx master page I have:
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Master.cs" Inherits="Master.Master" %>
Then I have a file called:
Master.cs
and the code below is:
namespace Master
{
using System;
public partial class Master : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("ALEX");
Response.End();
}
}
}
as far as I can see all my declarations are correct, but the error I get is:
ASPNET: Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl).
and the line:
Line 5: public partial class Master : System.Web.UI.Page
is in red. I have had this error before but I am sure I have all my code correctly, so am not sure why I m getting this error?
Maybe the master file needs a different setup, or it works differently on the masterpage?
When defining a master page code behind class, the base class to inherit is System.Web.UI.MasterPage, not System.Web.UI.Page.

Setting Page Async mode to true from Code-Behind

Is it possible within my code-behind file to set the asynchronous mode of the page directive.
I have no way of directly modifying the <%#Page %> attribute and and struggling to find a way to implement this in my code-behind.
I have tried in my Page_Load method to add Page.AsyncMode = true, but it returns the following error:
is inaccessible due to its protection level
Is there any way to do this? Without being able to directly modify the master page?
No, you cannot change the asynchronous mode of a page in the code-behind. An asynchronous page implements the IHttpAsyncHandler interface, and there is no way to change the interfaces implemented by your page after the .aspx file has been compiled by ASP.NET and your code is running.
Setting the Page.AsyncMode property will not change the asynchronous mode. Its purpose is to let controls on the page know whether the page is running in asynchronous mode or not, so tampering with the property may cause controls to malfunction.
My guess - you're trying to access this property from your master page. But according to documentation, this property is protected bool AsyncMode { get; set; }. Which means it is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member.
This property is declared in System.Web.UI.Page and is accessible in it and any class derived from it. MasterPage doesn't derive from Page. That's why you cannot access it.
You can easily access it from your page:
public partial class YourPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.AsyncMode = true;
}
}
Because you're MasterPage does not Inherit from your Page, you cannot access the AsyncMode Property.
If you absolutely must edit the value from your MasterPage maybe consider adding a method to your page called "UpdateAsyncMode" and doing the following from your masterpage Page_Load
protected void Page_Load(object sender, EventArgs e)
{
MyPageClass p = this.Page as MyPageClass ;
p.UpdateAsyncMode(true);
}
Alternatively if this is something that needs to be more robust you could create a base class for Pages like the following and have all the web pages in your site extend that base class
public abstract class MyBasePage : System.Web.UI.Page
{
public void UpdateAsyncMode (bool b)
{
this.AsyncMode = b;
}
}

MasterPage Load Theme

on asp.net C#,
i want to change the theme when page is load, this action requiers PreInit Event, Which Masterpage dont have. what is the solution for that issue?
thanks,
One way is to create an Http Module (Rick van den Bosch blogs)
Aside from an Module, you could consider having all your pages inherit from a base page that defines PreInit and sets the theme accordingly.
Edit for comment.
There isn't all that much to it, just make sure the pages you create inherit from a class similar to the BasePage class below instead of the default Page class and you should be set.
public class BasePage : Page
{
public BasePage()
{
this.PreInit += new EventHandler(BasePage_PreInit);
}
protected void BasePage_PreInit(object sender, EventArgs e)
{
this.Page.Theme = theme; //Garner from appropriate resource
}
}
Hope that helps.

Categories

Resources