I’m trying to add a JavaScript function to show all selected items from a ListBox as concatentated strings in a Label on the page. It’s needed because AutoPostBack=”true” will cause the ListBox to scroll all the way back to the first selected item.
So this code works:
<script type="text/javascript">
function Updatelist() {
var sel = document.getElementById('<%=lstbxStuff.ClientID%>');
var lbl = document.getElementById('ctl00_cph_lblSelectedStuff');
var listLength = sel.options.length;
var textForListbox = "";
var list2length = 0;
for (var i = 0; i < listLength; i++) {
if (sel.options[i].selected) {
if(list2length == 0) {
textForListbox = sel.options[i].text;
} else {
textForListbox = textForListbox + ", " + sel.options[i].text;
}
list2length++;
}
}
lbl.innerText=textForListbox;
return textForListbox;
}
</script>
Unfortunately I still need the code behind SelectedIndexChanged delegate. Is there a way to use both of these without doing a PostBack? When I set AutoPostBack=”false”, my delegate never seems to be reached.
If you want to call a server side deligate then you have to do a PostBack.
What is the code on the server that needs to be ran? You should be able to do all the work in JavaScript, then have a different trigger (not selectedIndexChange) to run the server side code once all the list items are selected.
Have you also seen, Ajax UpdatePanel and maintainScrollPositionOnPostBack=”true” so that the page retains it’s scroll position after postbacks. However this will only affect the pages scroll bar not the selectbox.