views:

999

answers:

4

I have an asp.net web page with a ton of code that is handled in the Page-Load event of the page. I also have a dropdown box on the page that should reload the page with a new value, but I would like to get this new value before I process the entire Page-Load code. I am trying to get my head around ASP.NET page lifecycle.

Should I move the Page-Load code to a later event or is there a way to get the value of the dropdown list value before the the Page-Load event begins?

TIA

+1  A: 

Try Page_Init

John Sheehan
Are you saying I can get the value of a dropdown box in the page_init?
zkent
The answer is more involved than this obviously, but this should point you in the right direction. If you're going to be working with webforms a lot, get very familiar with the page lifecycle. Maybe this will help: http://john-sheehan.com/blog/net-cheat-sheets
John Sheehan
Yes, you can get the selected value in Page_Init. You really want to be retrieving it in an event outside of Init/Load though (like a button click, etc).
John Sheehan
The problem with a button_click event is that it is processed AFTER the page_load where the majority of the code is being processed and I am hoping to avoid the un-needed processing if I am going to reload the page with new data based on the selection anyway.
zkent
A: 

As noted before, Page_Init is what you want. But I emplore you to make your pages as loosly coupled as posible. Look into the MVP pattern for starters. Also, make sure that most of your logic is in your domain objects.

There shouldn't be too much code in the Page_Load event. If there is, it shoud be broken up into concise methods so that you don't have crazy code.

Charles Graham
I inherited this code and I have refactored it immensely to move the code into classes and methods. However, I still want to read the value PRIOR to page_load
zkent
+2  A: 

I would use Page_PreLoad instead of Page_Init, because it is raised after all postback data is processed.

Martin Moser
I am having better luck with PreLoad than Init. I can't seem to get the SelectedValue of the ddl during Init but I can with preLoad.
zkent