Client-side Javascript + Server-side VB.NET = violent hair removal

  • Thread starter Thread starter adouglas
  • Start date Start date
A

adouglas

Guest
Im currently working on my first .NET project and Ive hit a bit of a snag. I have a .aspx page which, among other controls, contains a pair of asp:listboxes each accompanied by an html button. Clicking the html button beneath either list calls a bit of Javascript which moves all the selected items from that list over into the other list. This all works fine.

The problem is that when I click on the Save button to post the form back to the server, the VB.NET code on the server end is not seeing the changes to the listboxes that were made by the javascript. It just sees the listboxes as they were when the page was originally sent. Ive tried using Javascript to change the content of a textbox on the client end, and the VB.NET code sees those changes when the page is posted back to the server.

Does anybody know what I have to do to be able to see the changes the Javascript made to the listboxes from the server end?
 
The web, by nature, is a stateless environment. In order to get around this ASP.NET uses a feature known as ViewState to store information about a pages controls in a hidden form field. When the client submits the page back to the server it reconstructs the controls using this hidden form field (ViewState) and allows the programmer to interact with them easily. What youre doing by editing the controls client-side is invalidating the ViewState, and the server doesnt even know any changes were made. What youll need to do is make the changes server-side, as ASP.NET intended, or disable viewtstate for the page or for those controls.
 
Thanks for replying.

I tried disabling the Viewstate for the list controls in question, but now the server wont see any values in them at all. Is there any way for me to read the new value of the control from the server? In the old ASP I could just use Request.Form to view the value of any controls on the page that was sent to the server, but this doesnt seem to work in .NET. I can make it work if I change the page on the server, but its going to make things very, very slow for the user if it has to make a round trip to the server every time they want to copy something from one list to the other.

Out of curiosity, why does this only seem to affect certain controls? When I change the value of a TextBox or Checkbox at the Client end, either by typing directly into it or through Javascript, the code on the server doesnt have any trouble seeing the changes. So why doesnt it work with ListBoxes?
 
Back
Top