Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I do lot fo work by javascript on web page
(like validation , enable and disable
textbox ,changes the value of label) and
when we do a postback by buttonclick all
work done by java script is lost , pls
suggest how to do this asap. Thanks
Posted
Comments
F-ES Sitecore 9-Aug-15 7:05am    
You need to store the state of things in hidden fields, so if something is disabled you'll need to store that using some notation in a hidden field. So you might have a field called "DisabledItems" and in there you'll get a list of control IDs that are to be disabled. On postback you'll read that field, parse the list and ensure the controls are disabled. Or you could just use an updatepanel rather than doing things via javascript. Also remember that the user can get around your javascript if they want to, so ensure your validation is done server side also.
Sergey Alexandrovich Kryukov 9-Aug-15 11:28am    
There is a cleaner and more universal way; please see Solution 1.
—SA
F-ES Sitecore 9-Aug-15 18:11pm    
It depends on if the data is actually needed server-side or not, and how long the data has be exist for. Hidden fields has the advantage of the data only living as long as you want to, and it is available server-side, but it is obviously harder to code for.
Sergey Alexandrovich Kryukov 9-Aug-15 21:29pm    
That's why I described the alternative in my answer.
—SA

1 solution

In addition to what F-ES Sitecore advised in his comment to the question:

There is more modern, more straightforward, natural and universal alternative: using Web storage: http://en.wikipedia.org/wiki/Web_storage[^].

In your case, you would need to use sessionStorage object, to avoid contamination of the customer's browser's permanent data with temporary information It is more universal, first of all, because it can be used regardless of server side; this is a pure JavaScrip, client-side technique.

Also, to store the whole set of data stored in a single object by a single key, you can serialize and deserialize such object using native JSON object: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON[^].

These techniques are explained on an example in this section of my article: JavaScript Calculator, 7. Dynamic Strict Mode Switching and Web Storage.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900