Where should stuff be done in an ASP.NET page?

前端 未结 6 1493
名媛妹妹
名媛妹妹 2020-12-30 13:20

I\'m very new to ASP.NET and, after beating my head on a few problems, I\'m wondering if I\'m doing things wrong (I\'ve got a bad habit of doing that). I\'m interested in le

6条回答
  •  孤城傲影
    2020-12-30 13:32

    The links posted by various folks are very helpful indeed - the ASP.NET page life cycle really isn't always easy to grok and master!

    On nugget of advice - I would recommend preferring the overridden methods vs. the "magically" attached methods, e.g. prefer the

    protected override void OnLoad(EventArgs e)
    

    over the

    protected void Page_Load(object sender, EventArgs e)
    

    Why? Simple: in the overridden methods, you can specify yourself if and when the base method will be called:

    protected override void OnLoad(EventArgs e)
    { 
        base.OnLoad(e);
        // your stuff
    }
    

    or:

    protected override void OnLoad(EventArgs e)
    { 
        // your stuff
        base.OnLoad(e);
    }
    

    or even:

    protected override void OnLoad(EventArgs e)
    { 
        // some of your stuff
        base.OnLoad(e);
        // the rest of your stuff
    }
    

    or even:

    protected override void OnLoad(EventArgs e)
    { 
        // your stuff
        // not call the base.OnLoad at all
    }
    

    You don't have that flexibility in the Page_Load() version.

    Marc

提交回复
热议问题