Page_Load 与 OnLoad
为什么 DisplayUsers();
不起作用?
我的基本页面是:
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("/"); };
}
}
我的类是,
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();
}
}
但 DisplayUsers()
不起作用,
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,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我没记错的话,您需要调用基类的
OnLoad
事件来正确注册Page_Load
事件:以下是一些不错的读物:
If I recall correctly, you'll need to call the base class's
OnLoad
event to register thePage_Load
event properly:Here are a couple of good reads:
根据 .NET 应用程序中的性能提示和技巧:
According to Performance Tips and Tricks in .NET Applications:
在执行的代码中,没有区别,但是
Page_Load
(以及其他事件(类似这样)使用自动事件订阅机制,它使用反射,这会稍微影响性能,我个人建议重写
OnLoad()
,只是不要忘记调用base
。In the code executed, there is no difference, but
AutoEventWireup
should be enabled (usually in markup) for each pagePage_Load
(and other events like this) uses automatic events subscription mechanism, which uses Reflection, what slightly hits performanceI personally recommend to override
OnLoad()
, just don't forget to callbase
.