Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Page controls keeps same content after postback?

Page controls keeps same content after postback?

Scheduled Pinned Locked Moved ASP.NET
helpcsharpasp-netquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lvq684
    wrote on last edited by
    #1

    Hi. I am fairly new to asp.net, and have a problem with a page I am working on. The page contains some textboxes, and a button. When the button is clicked, the codebehind fetches the textbox contents. If the user did something wrong, he is redirected to the same page again, but before that the textbox contents is saved in the session. When the page loads again the textboxes are now filled with this sessiondata so the user doesent have to input all the data from scratch again. But when the button is clicked the second time, the textbox content I read is the old content, and not the new data the user has typed in. How come? To make my problem a bit easier to understand: 1. Page loads 2. User types "John" in the name textbox, and fills out other textboxes 3. User clicks the submit button 4. Codebehind reads "john" from the name textbox 5. Codebehind registers an error from the other stuff the user has filled in, and redirects to the same page with all textbox contents saved in session 6. Page loads, and all textboxes are filled from the session data 7. User changes the text in the name textbox to "Joe" 8. User clicks submit 9. Codebehind reads "john" from the name textbox - How come?! Really hope you can help me understand this! Thanks alot :)

    B A 2 Replies Last reply
    0
    • L lvq684

      Hi. I am fairly new to asp.net, and have a problem with a page I am working on. The page contains some textboxes, and a button. When the button is clicked, the codebehind fetches the textbox contents. If the user did something wrong, he is redirected to the same page again, but before that the textbox contents is saved in the session. When the page loads again the textboxes are now filled with this sessiondata so the user doesent have to input all the data from scratch again. But when the button is clicked the second time, the textbox content I read is the old content, and not the new data the user has typed in. How come? To make my problem a bit easier to understand: 1. Page loads 2. User types "John" in the name textbox, and fills out other textboxes 3. User clicks the submit button 4. Codebehind reads "john" from the name textbox 5. Codebehind registers an error from the other stuff the user has filled in, and redirects to the same page with all textbox contents saved in session 6. Page loads, and all textboxes are filled from the session data 7. User changes the text in the name textbox to "Joe" 8. User clicks submit 9. Codebehind reads "john" from the name textbox - How come?! Really hope you can help me understand this! Thanks alot :)

      B Offline
      B Offline
      Brij
      wrote on last edited by
      #2

      It should not happen.Might be the case when user changes the data in the textbox and click submit then on pageload you are again filling the data from session.Debug the code and check the values in immediate window you might the get the issue. One more thing, why you are taking the help of session, you can use viewstate.Enable the viewstate of the contrls on the page that will suffice your requirement.

      Cheers!! Brij Check my latest Article :URL Routing with ASP.NET 4.0

      A 1 Reply Last reply
      0
      • L lvq684

        Hi. I am fairly new to asp.net, and have a problem with a page I am working on. The page contains some textboxes, and a button. When the button is clicked, the codebehind fetches the textbox contents. If the user did something wrong, he is redirected to the same page again, but before that the textbox contents is saved in the session. When the page loads again the textboxes are now filled with this sessiondata so the user doesent have to input all the data from scratch again. But when the button is clicked the second time, the textbox content I read is the old content, and not the new data the user has typed in. How come? To make my problem a bit easier to understand: 1. Page loads 2. User types "John" in the name textbox, and fills out other textboxes 3. User clicks the submit button 4. Codebehind reads "john" from the name textbox 5. Codebehind registers an error from the other stuff the user has filled in, and redirects to the same page with all textbox contents saved in session 6. Page loads, and all textboxes are filled from the session data 7. User changes the text in the name textbox to "Joe" 8. User clicks submit 9. Codebehind reads "john" from the name textbox - How come?! Really hope you can help me understand this! Thanks alot :)

        A Offline
        A Offline
        Abhijit Jana
        wrote on last edited by
        #3

        lvq684 wrote:

        Really hope you can help me understand this!

        can you Please show us the code block ?

        Cheers ! Abhijit Jana | MVP Web Site : abhijitjana.net | Follow Me @ twitter Read my Latest Article :Mastering Debugging in VS 2010

        L 1 Reply Last reply
        0
        • B Brij

          It should not happen.Might be the case when user changes the data in the textbox and click submit then on pageload you are again filling the data from session.Debug the code and check the values in immediate window you might the get the issue. One more thing, why you are taking the help of session, you can use viewstate.Enable the viewstate of the contrls on the page that will suffice your requirement.

          Cheers!! Brij Check my latest Article :URL Routing with ASP.NET 4.0

          A Offline
          A Offline
          Abhijit Jana
          wrote on last edited by
          #4

          Brij wrote:

          One more thing, why you are taking the help of session, you can use viewstate.Enable the viewstate of the contrls on the page that will suffice your requirement.

          Relation of textbox and ViewState is something different with respect to other controls. If you make EnableViewState=False; for Textbox, still it will maintain the postback data. Why ? First of all you need to know what actually happened during ASP.NET Page life cycle and ViewState. In the ASP.Net page life cycle, two events related with View State. 1. Load View State : Where view state data is loading for the control and which happened before Page_Load. 2. Save View State : Where control data store in a hidden field before the Page_Render. Now, If you disable the ViewState for the control it should store the view state data but, you are saying in Textbox, after you disabled the view state , control holds the data after postback, Here is the Trick, There is another event fired during Page Life Cycle, which is, Load PostBack Data ASP.NET controls which implement IPostBackEventHandler will load its value from the appropriate postback data. This value is not read from view state but from Postback From and this is true for those control which implements the IPostBackEventHandler and TextBox is one such control. Hope you got my point.

          Cheers ! Abhijit Jana | MVP Web Site : abhijitjana.net | Follow Me @ twitter Read my Latest Article :Mastering Debugging in VS 2010

          B 1 Reply Last reply
          0
          • A Abhijit Jana

            Brij wrote:

            One more thing, why you are taking the help of session, you can use viewstate.Enable the viewstate of the contrls on the page that will suffice your requirement.

            Relation of textbox and ViewState is something different with respect to other controls. If you make EnableViewState=False; for Textbox, still it will maintain the postback data. Why ? First of all you need to know what actually happened during ASP.NET Page life cycle and ViewState. In the ASP.Net page life cycle, two events related with View State. 1. Load View State : Where view state data is loading for the control and which happened before Page_Load. 2. Save View State : Where control data store in a hidden field before the Page_Render. Now, If you disable the ViewState for the control it should store the view state data but, you are saying in Textbox, after you disabled the view state , control holds the data after postback, Here is the Trick, There is another event fired during Page Life Cycle, which is, Load PostBack Data ASP.NET controls which implement IPostBackEventHandler will load its value from the appropriate postback data. This value is not read from view state but from Postback From and this is true for those control which implements the IPostBackEventHandler and TextBox is one such control. Hope you got my point.

            Cheers ! Abhijit Jana | MVP Web Site : abhijitjana.net | Follow Me @ twitter Read my Latest Article :Mastering Debugging in VS 2010

            B Offline
            B Offline
            Brij
            wrote on last edited by
            #5

            You are right Abhijit. But Load PostBack Data is called only for input controls those implement IPostBackEventHandler . For rest of the controls, viewstate could be used. Thanks for adding it in detail.

            Cheers!! Brij Check my latest Article :A walkthrough to Application State

            1 Reply Last reply
            0
            • A Abhijit Jana

              lvq684 wrote:

              Really hope you can help me understand this!

              can you Please show us the code block ?

              Cheers ! Abhijit Jana | MVP Web Site : abhijitjana.net | Follow Me @ twitter Read my Latest Article :Mastering Debugging in VS 2010

              L Offline
              L Offline
              lvq684
              wrote on last edited by
              #6

              Thanks alot for all your answers. I´ve tried with the viewstate, but It didnt really work. It prolly would have if I tried for a longer time ;) Instead I found out that the page load method is called several times, even if the page isnt loaded. So in that way the old values were set. But how come the page_load method is called often, even if the user hasnt done anything on the page? Thanks again :)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups