Thursday, May 15, 2008

Help, my database updates don't stick

Just a quick post to remind people of the life cycle. Most people learn this early on in their ASP.Net life, but it's something that can be overlooked and leave you slapping your head in frustration. Recently, I was working on a simple page that has some text box fields, which are pre-populated with a users details, you edit those and click submit, it submits the new details and refreshes the page, hopefully with the new details. My database updates didnt seem to work, I searched through my data access layer and business logic layer to no avail, then I slapped my head and mouthed a few curse words when I realized that I'd populated my text boxes in the Page_Load section, when really I should have done it on the OnPreRender or a similar event (or, as mentioned in comments, use the !Page.IsPostBack method, though this way would simply retain the data inserted into the text boxes from viewstate, I wanted it to regenerate it from the database to ensure my data was inserted correctly and in the format I wanted) To brush up on the events life cycle check out this article.

1 comment:

Anonymous said...

You can do it in Page_Load just make sure you wrap an "if (!IsPostBack)" around it. That's the standard approach.