Results 1 to 4 of 4

Thread: [RESOLVED] Prevent data loss in Postback

  1. #1

    Thread Starter
    Addicted Member scsfdev's Avatar
    Join Date
    Feb 2008
    Location
    Singapore
    Posts
    224

    Resolved [RESOLVED] Prevent data loss in Postback

    Hi all,

    Want to ask some answers for my current problem.
    I have a page which has dynamically created controls on it.
    1. One Place holder. (already on page)
    2. Two Tables which includes data and textboxes. (Created dynamically and use PlaceHolder.Controls.add to add it on form)
    3. One "Save" Asp.net button. (Already created on page but use PlaceHolder.Controls.Add to add it and move its location.)

    User will fill in some information in the tables' textbox. There will be 12 text box per row and total over 400 rows. (Of course, user will not fill up all textboxes. Just a few 10 or 15 rows and user will enter remaining value in 'The rest: ' textbox.)

    When user finish, he will press the "Save" button.
    Problem is after pressing this button, it didn't go to button click event first. It goes to Page_init event first and then comes to button click event.
    So, in the middle of somewhere, the form is brand new and there is no data inside the textbox when it reached button event.

    How to prevent this from happen?

    What I found so far is:
    I read some post indicate to declare the dynamically controls in Page_Init event instead of Page_Load event. So, I did it. And so when pressing button, it goes to Page_Init and so my dynamic controls all become new.

    Any solution?

    Thanks.
    I'm using VS 2005 & 2008 & 2010 with SQL Server 2005 Express.

    My hobby beside programming: http://dslrstranger.wordpress.com

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Prevent data loss in Postback

    That's normal behavior. When any post back occurs, it'll first do the usual Page_Init, Page_Load and then get to your event. The idea here is that if you need to perform any initializations or resource settings, you do it in those events. Read more about the ASP.NET Page Life Cycle

    If you want to avoid certain things from happening on each postback, use Page.IsPostBack which is a boolean. If you have dynamically generated controls, regenerate them in Init or Page Load so that viewstate can be assigned to them and you don't lose the values.

  3. #3

    Thread Starter
    Addicted Member scsfdev's Avatar
    Join Date
    Feb 2008
    Location
    Singapore
    Posts
    224

    Re: Prevent data loss in Postback

    Quote Originally Posted by mendhak View Post
    That's normal behavior. When any post back occurs, it'll first do the usual Page_Init, Page_Load and then get to your event. The idea here is that if you need to perform any initializations or resource settings, you do it in those events. Read more about the ASP.NET Page Life Cycle

    If you want to avoid certain things from happening on each postback, use Page.IsPostBack which is a boolean. If you have dynamically generated controls, regenerate them in Init or Page Load so that viewstate can be assigned to them and you don't lose the values.

    Thanks mendhak.
    I'm using VS 2005 & 2008 & 2010 with SQL Server 2005 Express.

    My hobby beside programming: http://dslrstranger.wordpress.com

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] Prevent data loss in Postback

    No problem, hope that helped... in some way.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width