Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

data only writes if (!IsPostBack) is removed from page_load

Status
Not open for further replies.

tigerjade

Programmer
Mar 17, 2004
237
0
0
US
I know, it sounds strange, and I'm sure it's because I don't understand postbacks as well as I should. Here's the deal: I have a survey I've been working on that's completely db-generated; I'm just reloading one page with new data after they complete a section. The submit button's onclick event goes through the controls, pulls out the data and writes it, and then I use response.redirect to refresh the page with the new section ID.

If I leave this in Page_Load:
Code:
if (!IsPostBack)
{
    BindData();
}

the page goes to the next section but it does not write anything to the db. If I comment out the postback condition, it writes the data TWICE to the db & then goes to the next section.

What am I doing wrong?

Thanks!

tigerjade

"Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live." -- Martin Golding


 
I agree with jbenson. I dunno if it helps but one thing that threw me off for a while is that when a control has auto postback enabled, it will execute the page_load and then the event handler. If using Visual Studio, try marking the left margin w/ one of those big dots it puts when you click on it, then start debugging (F5). Once it hits the marked spot you can trace it through line by line using the F11 key. (Your project configuration must be set to debug and not release for this to work).
 
You should use the Page Life Cycle.

1. Page_Load
2. Button and other control Events fire
3. (the most unknown event)Page_PreRender

If you bind your datagrids on Page_PreRender then you don't have to worry about maintaining page state.

2. The button clicks. You change and adjust the datasource based upon the user input of the page.

3. You bind the datasource to the grid.

This way your always using post backs as opposed to response.redirects where you loose all your viewstate.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top