簡體   English   中英

Page_Load 與 OnLoad

[英]Page_Load vs 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("/"); };
    }        
  }

我的 class 是

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()不起作用,

如果我沒記錯的話,你需要調用基類的OnLoad事件來正確注冊Page_Load事件:

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);
}

這里有幾個很好的讀物:

根據.NET應用程序中的性能提示和技巧

避免使用Autoeventwireup功能

而不是依賴autoeventwireup,覆蓋頁面中的事件。 例如, 不要編寫Page_Load()方法而是嘗試重載public void OnLoad()方法。 這允許運行時必須為每個頁面執行CreateDelegate()。

在執行的代碼中,沒有區別,但是

  • 應為每個頁面啟用AutoEventWireup (通常在標記中)
  • Page_Load (和其他類似的事件 )使用自動事件訂閱機制 ,它使用Reflection,稍微點擊性能

我個人建議覆蓋OnLoad() ,不要忘記調用base

使用 JSON JavaScriptSerializer 進行序列化或反序列化時出錯。 字符串的長度超過了 maxJsonLength 屬性上設置的值。 說明:在執行當前 web 請求期間發生未處理的異常。 請查看堆棧跟蹤以獲取有關錯誤及其源自代碼的位置的更多信息。

異常詳細信息:System.InvalidOperationException:使用 JSON JavaScriptSerializer 進行序列化或反序列化期間出錯。 字符串的長度超過了 maxJsonLength 屬性上設置的值。

源錯誤:

當前 web 請求的執行過程中產生了一個未處理的異常。 可以使用下面的異常堆棧跟蹤來識別有關異常起源和位置的信息。

堆棧跟蹤:

[InvalidOperationException:使用 JSON JavaScriptSerializer 進行序列化或反序列化期間出錯。 字符串的長度超過了 maxJsonLength 屬性設置的值。] CSRL.SMT.SampleProduction.SampleProductionRequest.InitializeCombo() +1190 CSRL.SMT.SampleProduction.SampleProductionRequest.Page_Load(Object sender, EventArgs e) +94 System.Web。 Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +52 System.Web.UI.Control.OnLoad(EventArgs e) +97 System.Web.UI.Control.LoadRecursive() +61 System.Web.UI.Page. ProcessRequestMain(布爾包含StagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)+693

請幫我

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM