tags:

views:

143

answers:

10

Why is knowing the Asp.net lifecyle important to coding in Asp.net?

+10  A: 

Because otherwise, you will end up making false assumptions about your code.

It is never a good idea to develop for a platform without understanding how that platform works.

At the very least, all ASP.Net developers must understand the difference between client-side code (Javascript) and server-side code (C#), and how the postback model works.
It is also a good idea to understand ViewState and session.

SLaks
So let me understand. I have been coding in ColdFusion a long time. I understand the difference between Client-Side Code and Server-Side Code. I also understand the settings pre-postback and postback. What else am I missing. ViewState and/vs/or Session?
Nathan Stanford
A: 

At least you should be aware that ASPX with a page lifecycle carries a lot of overhead and choose something simpler such as a Http handler (ashx) when appropriate.

Henrik
+6  A: 

Firstly, knowing how anything works should be a pre-requisite to being able to code properly in it, otherwise it is just guesses and good luck. The ASP.NET lifecycle is a core part of how ASP.NET works and so is necessary knowledge.

What sort of things, specifically, can go wrong if you don't know?

Some examples:

  • If you don't understand the order of events, you can end up reading properties of controls before they've been restored from ViewState
  • You can end up trying to write output when it is too late (e.g. after pre-render time)
  • You can end up writing to properties to early, only to have them overwritten by something else.
  • You can have problems with trying to do things before child controls have been created.

And many more. In short - it matters, that's why you need to know it.

Rob Levine
+4  A: 

<BadAnalogy>For exactly the same reason that knowing how page numbering works is important to reading a book</BadAnalogy>

A lot of the time you can get by perfectly fine without understanding the page lifecycle, however every now and then you will encounter something that you can't explain unless you understand the lifecycle - this will happen with increasing frequency the more advanced you get.

Besides, its not that difficult (you don't need to know it, just understand it), and having a good grasp of how the page lifecycle works will give you a much greater understanding of how ASP.Net works, which in turn will make using ASP.Net that much easier.

Kragen
If you are going to down-vote, please explain why!
Kragen
I think this is a great point. It tries to abstract the web away from the programmer, so that they live in a little cloud cookoo land place where controls are dragged onto forms and events are attached to buttons and properties are set and everyone is happy and incedently you are absolutely tied into a runtime, and a development environment as a result.
James Westgate
+1 It *was* a bad analogy, but your point was still correct as far as I am concerned. Doesn't really deserve downvotes.
Rob Levine
@James, this isn't always a bad thing - unlike many simple drag and drop environments that people get dragged into, ASP.Net is also a very competent environment that "scales" depending on how advanced the "developer" (used in the broadest of terms) is.
Kragen
@Kragen - I disagree completely. No developer should be forced to learn this. It is in no way helping them to understand the web, or support them in creating complex applications. asp.net applications quickly become a mine-field of complexity (I know from bitter experience) that simply doesnt have to exist. Rather let the developer learn semantic clean markup, css and a server-side or client-side language that allows him/her to intelligently manipulate the markup.
James Westgate
@James: One can be perfectly content not using any of those things you just mentioned and make a perfectly good website, especially for internal purposes. You don't have to be a DOM warrior to build a website and that's a good thing if you want it.
Greg
A: 

The lifecycle and viewstate are two of the most important aspects of ASP.net. Without knowing the lifecycle you will run into many issues with viewstate, dynamic controls, events ect. Knowing it will save you plenty of time!

Mike
A: 

Knowing the asp.net life cycle means knowing what, when and where: what to do when an event is fired and where will it be applied.

You need to know not just for the "saving a lot of time" reason, but for building a website with good practice in mind.

Happy Coding

GxG
A: 

ASP.NET tries to provide you state over a completely stateless protocol, HTTP. Understanding the page lifecycle is extremely important because if you don't understand the lifecycle, you will end up debugging a bunch of code almost unnecessarily.

For ex. if you are using dynamic controls, and adding them in Page_load, the events may not fire. The same controls and events will work fine if you initialize the code in Page_init.

So, you can clearly see that just by knowing which event to use for what kind of stuff... you can avoid getting into unnecessary loops of learning. Check this out...

http://msdn.microsoft.com/en-us/library/ms178472.aspx

Rahul Soni
+2  A: 

Hey,

Because ASP.NET uses a complex object model, it's important to know because:

  • You can troubleshoot or learn to take advantage of viewstate.
  • It's good to know for user controls or custom controls (since they also have the same lifecycle minus some of the page lifecycle events). User controls can interact with the page through this lifecycle.
  • It's helpful to debug state issues due to components changing their values (it was value X earlier, now it's value Y this postback lifecycle, did it come back from the server that way, or was it changed during load time?)
  • You can learn how to build custom components to take advantage of the lifecycle, and reduce your overall coding effort (using a module or a custom page class, or something).

HTH.

Brian
A: 

My first couple of asp.net applications were written with minimal knowledge of the lifecycle, but they were only small applications (a simple photo gallery and a mapping app).

As soon as you start creating anything useful in a business environment, you'll want to use more of the framework and you'll need to look at some lifecycle diagrams to understand some of the error messages you will inevitably get.

Paul Spangle
A: 

There's a reason Visual Studio generates the Page_Load handler for you - you can make a perfectly serviceable website using only that even in the page lifecycle.

Knowing about the lifecycle (and how to look up a reference when you need it) is important because eventually you'll do something in the Page_Load that doesn't work. Probably the first thing you'll run into is that control events happen Page_Load and you'll want to do something after those control events. So you'll load up the good ol' Page Lifecycle reference and see that Page_LoadComplete comes after that, so you handle that even and everything works great again.

Greg