dcsimg
Results 1 to 4 of 4

Thread: Form losing user input on postback

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2018
    Posts
    2

    Form losing user input on postback

    I have a form that loads data from DB and populate the fields upon Page_Load event. Then the user can go through the form and change the information at his will.

    The problem is that when the submit button is clicked at the end, the page reloads and therefore the Page_Load event is triggered again repopulating the fields with the DB data and obviously overwriting the user inputs. So basically the same data that was loaded from DB is saved back to it. Useless.

    I tried the viewstate approach (preserved the data at the submit button click event and tried to retrieve it back at the end of the Page_Load) like this:

    Code:
    sub button_click
        viewstate("myfield") = myfield.value
    end sub
    
    sub page_load
       'load stuff from DB here
       myfield.value = viewstate("myfield")
    end sub
    However it didn't work for me.

    I was thinking that maybe my mistake is populate the fields on the Page_Load event. There is any other event where I can do that instead? If not what would be your approach to not lose the fields data upon postbacks?

    Thanks!
    Last edited by Yankleber; Dec 21st, 2018 at 04:20 PM.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    33,740

    Re: Form losing user input on postback

    Since you are talking about Page_Load, I assume this is ASP.NET, so I moved the thread. I expect you'll get better answers in the ASP.NET forum, as the problem sounds specific to web technology.
    My usual boring signature: Nothing

  3. #3
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,024

    Re: Form losing user input on postback

    In the Form_Load event you can check the Page.IsPostBack property to detect if you are dealing with a postback, if the property is true then don't reinitialise everything on the page.

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2018
    Posts
    2

    Re: Form losing user input on postback

    Quote Originally Posted by PlausiblyDamp View Post
    In the Form_Load event you can check the Page.IsPostBack property to detect if you are dealing with a postback, if the property is true then don't reinitialise everything on the page.
    Cool, thank you very much, it fixed the issue!

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width