Page controls keeps same content after postback?
-
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 :)
-
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 :)
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
-
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 :)
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
-
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
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
andViewState
. 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 implementIPostBackEventHandler
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 theIPostBackEventHandler
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
-
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
andViewState
. 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 implementIPostBackEventHandler
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 theIPostBackEventHandler
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
You are right Abhijit. But
Load PostBack Data
is called only for input controls those implementIPostBackEventHandler
. 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
-
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
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 :)