I have a hidden field that I set in my javascript code:
What you are missing is something along the lines of JQuery post.
Web applications are stateless.
As with some of the earlier posts, it is not sufficient on the server side to get the value of a control on the client without some postback event. The server has to know what is going on. In your client-side code, you get the value of the control in your script block, but there is no post to the server to let it know what transpired in the client code. You need to understand that either you post the form (or at least the data) back to the server or ,if you want this to happen without a full postback, you need to learn JQuery or Ajax.
If you need this to happen without some user event, then do the post in the document.ready function. However, that is redundant because your server posted the data in the first place, so without user interaction (in document.ready) there isn't anything the server won't already know, especially in a hidden value.