Items not added to ListBox after using jQuery .appendTo

后端 未结 2 448
广开言路
广开言路 2021-01-26 11:35

Preface: First time really using JavaScript + jQuery, so my problem likely stems from a lack of understanding.

I have some very simple Javascript to move elements from o

相关标签:
2条回答
  • 2021-01-26 12:06

    This is actually a limitation that is due to the fact that .NET assumes that everything is left the way it was first served. If the site renders a textbox, it'll know that the value may have changed, and it updates its value based on the POST data. If it renders a disabled textbox, for instance, it "knows" that the value cannot have changed, and so, you will not be able to get the changed text, even if a javascript has enabled the textbox.

    The same thing is happening here. .NET assumes it already knows what items belong to which list. The selected value is always being posted in the Request.Form collection, so even if the values in the .NET listbox control doesn't change, you will still be able to fetch the values that way (Request.Form[myListBox.ClientID]), but the values that are not selected in any of the listbox, are not being passed as form data, so there is actually technically no way for the server to know in which ways you tampered with the listbox before submitting it.

    What you have to do isn't very pretty, but it's the only way. In both of your functions that move data around, you'll have to make sure that they also update a hidden field. It could be one hidden field for each listbox, or one hidden field containing the comma separated values of all listbox items, in a pipe-separated list of all listboxes, or whichever way you want to represent the data, but at the end of the day, you really have to manually make a string representaiton of the data in the different listboxes, that you can pick up at the serverside, and use to re-bind your listboxes with the new, relevant data.

    0 讨论(0)
  • 2021-01-26 12:31

    If you are using asp.net I believe this is because you are not manipulating the viewstate, and this is important to the asp.net server on the postback to render.

    I would suggest simply using an UpdatePanel to wrap your controls. Asp.net has great Ajax type functionality built in already, no full postback necessary. Their logic controls all the name mangling and viewstate.

    0 讨论(0)
提交回复
热议问题