2

I have been reading about the Page LifeCycle. I understand the LifeCycle, however, it's not clear on what to do, and when to do it. The problem is I use Page_Load to get database values, and set form fields. I use a button's onClick method to update the database. However, the form fields text properties were set during Page_Load, so it's really only updating the database with the OLD values.

Page_Load: I gather data, and set control text properties to reflect data. Button_onClick: I update the database from the form Problem: It's updating values gathered from Page_Load and not the actual form.

Certainly, I am not supposed to perform everything in the Page_Load. So where am I going wrong during this process?

Michael C. Gates
  • 982
  • 1
  • 6
  • 18
  • 1
    Don't forget to use !Page.IsPostback in your Page_Load to make sure you only load your initial values the first time you visit the page. – AaronS Dec 08 '11 at 15:45

1 Answers1

0

Page_Load

If you are loading your database data in the Page_Load event, the very first thing to do is to wrap it within a if (!IsPostBack) statement.

IsPostBack
Gets a value that indicates whether the page is being rendered for the first time or is being loaded in response to a postback.

http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx

So IsPostBack = true when the page cycle is the result of postback.

In your Page_Load, you should only gather your data when IsPostBack = false, not on every page load.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // gather your data here
    }
}

Setting fields

I personnaly prefer to set the fields content on the PreRender event handler (but honnestly i don't know it should/must be done there, it just seems more logic to me).

PreRender is executed after your postback events (click on a button, drop-down selection change...) so it ensures that your updates and more generally data modifications are done before rendering the page.

Didier Ghys
  • 30,396
  • 9
  • 75
  • 81
  • Wrong. The very first thing is to evaluate whether or not the page load event is needed or if it can even be used. Also, there are many situations where adding !IsPostBack will **brick** your code. – O.O Dec 14 '11 at 21:20
  • Well, i'm not an expert for sure. Can you point me to some article/practical examples ? – Didier Ghys Dec 14 '11 at 21:28
  • http://stackoverflow.com/questions/6384613/aspxgridview-performcallback-does-full-page-postback – O.O Dec 14 '11 at 21:48
  • http://stackoverflow.com/questions/6270085/hidden-value-assigned-in-js-lost-after-postback – O.O Dec 14 '11 at 21:48
  • Honnestly, I don't get the relation between your comment and those two links. The first one concerns the use of a commercial component (ASPxGrid) that specifically uses a callbacks mechanism (so IsClassback should be used over IsPostBack). The second mention PageLoad on how it works with HiddenField for which no data is populated at load and changed via javascript... From someone down-voting and saying I'm wrong, I was expecting better input. – Didier Ghys Dec 14 '11 at 22:09