开发者

Page_Load not loaded after inheriting my own Page

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 开发者_Python百科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 ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜