handle event before Page_Load
Asked Answered
K

3

8

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

Kati answered 17/12, 2008 at 7:24 Comment(0)
P
11

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

Pirogue answered 17/12, 2008 at 8:2 Comment(1)
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.Kati
N
2

Try Page_Init

Nady answered 17/12, 2008 at 7:38 Comment(4)
Are you saying I can get the value of a dropdown box in the page_init?Kati
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: john-sheehan.com/blog/net-cheat-sheetsNady
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).Nady
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.Kati
T
0

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.

Thankful answered 17/12, 2008 at 7:58 Comment(1)
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_loadKati

© 2022 - 2024 — McMap. All rights reserved.