9

Why DisplayUsers(); doesn't work?

My base page is:

public class adminPage : System.Web.UI.Page
{
    protected override void OnLoad(EventArgs e)
    {
        if (User.Identity.IsAuthenticated == false) { Response.Redirect("~/Account/login.aspx?ReturnUrl=/admin"); };
        if (!(User.IsInRole("admin") || User.IsInRole("super user"))) { Response.Redirect("/"); };
    }        
  }

my class is

public partial class users : adminPage
{ 
    protected void Page_Load(object sender, EventArgs e)
    {                        
        string sName;
        adminGeneralData.GetToolData(2, out sName);
        pageH1.Text = sName;

        DisplayUsers();
    }

    protected void DisplayUsers()
    {
        DataSet ds = userData.GetUsersData();
        userList.DataSource = ds;
        userList.DataBind();
    }
}

but DisplayUsers() doesn't work,

Kev
  • 118,037
  • 53
  • 300
  • 385
eyalb
  • 2,994
  • 9
  • 43
  • 64
  • 1
    I don't really understand the use of UI.Page as baseclass instead of doing a real business logic. This is a web presentation app. All base logic should be separated from the presentation. – Independent Mar 19 '11 at 19:38
  • @dingir - give me an example! – eyalb Mar 19 '11 at 19:48

3 Answers3

19

If I recall correctly, you'll need to call the base class's OnLoad event to register the Page_Load event properly:

protected override void OnLoad(EventArgs e)
{
    if (User.Identity.IsAuthenticated == false) { Response.Redirect("~/Account/login.aspx?ReturnUrl=/admin"); };
    if (!(User.IsInRole("admin") || User.IsInRole("super user"))) { Response.Redirect("/"); };

    base.OnLoad(e);
}

Here are a couple of good reads:

Community
  • 1
  • 1
Metro Smurf
  • 37,266
  • 20
  • 108
  • 140
4

According to Performance Tips and Tricks in .NET Applications:

Avoid the Autoeventwireup Feature

Instead of relying on autoeventwireup, override the events from Page. For example, instead of writing a Page_Load() method, try overloading the public void OnLoad() method. This allows the run time from having to do a CreateDelegate() for every page.

Ryszard Dżegan
  • 24,366
  • 6
  • 38
  • 56
1

In the code executed, there is no difference, but

  • AutoEventWireup should be enabled (usually in markup) for each page
  • Page_Load (and other events like this) uses automatic events subscription mechanism, which uses Reflection, what slightly hits performance

I personally recommend to override OnLoad(), just don't forget to call base.

abatishchev
  • 98,240
  • 88
  • 296
  • 433