I have a ul with 10 li’s which the user has populated through jquery sortable. Each li has a hidden span with the ID in it. How do I read the IDs and pass them into my C# code behind when the user clicks an ASP.NET button?
Here is my thought process:
- Build a jquery function that loops and retrieves all the IDs and passes them into an array.
- Pass only the array back to C#.
- In my code behind: read the array and do whatever I need from there.
My problem:
- I don’t know what jquery is involved to create the array
- I don’t know how to pass the array to the code behind
- I don’t know how to read the array in my code behind
Any help is appreciated.
Thanks!
<li class="ui-state-default">
<span id="competencyID">3</span>
<h1><span id="competencyTitle">Comp Title</span></h1>
</li>
<li class="ui-state-default">
<span id="competencyID">18</span>
<h1><span id="competencyTitle">Comp Title</span></h1>
</li>
<li class="ui-state-default">
<span id="competencyID">103</span>
<h1><span id="competencyTitle">Comp Title</span></h1>
</li>
<li class="ui-state-default">
<span id="competencyID">6</span>
<h1><span id="competencyTitle">Comp Title</span></h1>
</li>
<li class="ui-state-default">
<span id="competencyID">25</span>
<h1><span id="competencyTitle">Comp Title</span></h1>
</li>
First you need to make id’s unique, I would replace the id’s of the
<span>tags with classes.Change:
To:
For your jQuery you can do something like this:
Here is a jsfiddle of collecting the id’s and adding them to a JavaScript object: http://jsfiddle.net/rre47/1/
–UPDATE–
If you want to return an array of ids that can more easily be parsed by server-side scripts you can use the following code:
Change:
var arr = {};tovar arr = {'id' : []};And Change:
$.param(arr)todecodeURIComponent($.param(arr))The output will look like this:
jsfiddle of the above code: http://jsfiddle.net/rre47/4/