Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Communication between client script and server code 1

Status
Not open for further replies.

JCruz063

Programmer
Feb 21, 2003
716
US
Guys,
I have a ListBox server control whose items are changed (added/removed) in the client by javascript. I'm aware that javascript changes live on the client only and server code is unaware of them. However, I need to save the client changes on the server and thus my server code needs a way to access them. Can anybody tell me how to implement this? In other words, how can I send the items in the ListBox to the server given the fact that the items were added through javascript?

Thanks!

JC
 
Hi JC

AFAIK if you change the items in a list and then postback the form then you should be able to pick the changes up from your server code in the same way that if you change the value of a textfield you can.

I hope you can as I'm speccing an app at the moment that will use this extensively and it's a bit late to go back to the drawing board on screen designs and the like....

Let me know if it works for you. I don't have time to knock a proof of concept now as my train home is in a minute (yay!) but i will have go later/tomorrow...

Rob

Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
I dont think you can unless you want to lose the DropDown events and start using Request.Form....

________
George, M
 
crazyboybert,
It doesn't work! Changes made through javascript live on the client and the server code is unaware of them. The items in the list must be saved into a structure whose modification are visible both in client script and server code. That's what I don't know how to do. Can anybody help out?

Thanks!

JC
 
All the form items are stored in the __VIEWSTATE, if you knw how to decrypt info... Actually it's base64 encoding. You would need to decode and encode all the __VIEWSTATE value.

________
George, M
 
Hi JC

Yes your absolutely right it doesnt work. Not to worry there are lost of ways around this. At the moment I'm thinking of adding some pre-postback javascript to capture the changes to the select list and store these in a hidden field. As an example say you add the following options to your dropdownlist using javascript
Code:
new Option('new one', '1');
new Option('new two', '2');
new Option('new three', '3');
In the above the values of the new options (1,2&3) may be IDs which reference something in your database or business objects (thats the case for me anyway).

Grab these and whack the in a HtmlInputHidden field with runat="server" as a csv and then do your processing from there on the server.

if you want to add items to differing locations in the list rather than to an empty or to the end of an existing list then you could build the csv string in the hidden input as you add the new options rather than as a pre-postback function.

If your unsure what I mean I can post some code as I have a POC for this running ok.

Rob

Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
crazyboybert,
Thanks for your reply. I understand what you mean, but I have a question to ask you:

Do you have any idea why the HtmlInputHidden control would not show up on my toolbox. I have an appropriate reference to the System.Web.dll assembly (which is where this controls is) yet it doesn't show up on the toolbox. I tried to add the control to the toolbox by right-clicking it and clicking "Customize ToolBox..." but HtmlInputHidden is not in the list of available .NET Components. I clicked the browse button (in the customize toolbox dialog box) to add the System.Web.dll file and still HtmlInputHidden doesn't show up. Worse yet, the System.Web.UI.HtmlControls namespace, which is where the HtmlInputHidden class exists is not even listed either. Any idea why this may be the case? I do have a Hidden control in the HTML tab of the toolbox and I was able to use this with a "runat=server" attribute to accomplish what I wanted. Yet, it puzzles me that the HtmlInputHidden control is not listed.

Ah, and one more thing. Any ideas on how I can add leading spaces to the items of a ListBox? When I add blank spaces at the beginning of the text, the list box trims the spaces. I need the spaces to line up the text with other items in the list. Using & n b s p ; doesn't work because the list displays the string "& nbsp;" (without the space between the & and n) instead of a blank space. Any ideas?

Thanks!

JC

We don't see things how they are; we see them how we are.
 
JC

I think the HtmlInputHidden Control does not appear in your toolbox because there isn't an asp.net Control as such. When you declare a HtmlControl in your code-behind this just creates a link to the HTML input field in the outputted page. The method you have used of selecting a standard HTML hidden field and adding the runat server attribute is normal.

On this listbox problem. I had a play with this after reading your other post on it. My initial thought was to try & nbsp; (no space) like you but this doesnt seem to work. If you add the & nbsp; to your aspx page in a listitem it has the effect desired but if the listitem is programatically added from code-behind then this doesnt work :-(

At the moment I can only suggest another character, maybe an underscore, hyphen or similar? I'll let you know if I can find a way to get the spaces in.

Rob

Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top