...

View Full Version : ListBox in Multiple mode Scroll problem



Aeron
03-02-2006, 12:26 PM
Hi,

I have a ListBix (data bound) and has AutoPostBack activated, so user clicks on an item and it refreshes some other list/table on page.

All works fine in single select mode. The page is refreshed and the selected items become the first one in a long list that has a scroll bar (it automatically scrolls to it ! which is great...).

But I need "Multiple" select mode too. In this case after single item is selected (or multiple items) the list does not scroll to any of them, it just refreshes the screen and stay at the top of the list (no scrolling).

I have tried everything with ASP.NET, with javascript code but no success. (Can you scroll with javascript ?)

Any help will be appreciated (I am guessing this is a common problem with Listbox + AutoPostBack + Multiple mode in ASP.NET).

Thanks

Aeron
03-02-2006, 06:12 PM
Hi,

I found a way of doing this, I use this Javascript function below and then call it at page_load of ASP.NET (this is for ListBox1+Multiple+AutoPostBack):

Function autoscroll()
{
var lst = From1.Listbox1
if ((lst.multiple==true) && (lst.length>0)) {
if (lst[lst.length-1].selected==true) {
lst[lst.length-1].selected=true;
return;
}
else {
lst[lst.length-1].selected=true;
lst[lst.length-1].selected=false;
}
for (var i=0; i< lst.length;++i)
if (lst.selected==true) {
lst.selected=true;
return;
}
}}

(Note that the "lst.selected=true" moves/scrolls the ListBox, also I select and deselect the last item so that the clicked item(s) go to the top of the list.)

*** One important thing You need to call it in ASP.NET like this:

Page.RegisterStartUpScript("MyScript","<Script language=javascript defer='true'> autoscroll() </script>")

("defer" is very important and also RegisterClientScriptBlock doesn't sometimes work correctly)

gg67
05-06-2008, 02:19 PM
Hello,

thx a lot. really useful.

But 2 questions :

1. is it really lst.selected=true; or lst[i].selected = true;
used lst[i].selected = true, and it seems to work ...

2. what about the "defer" attribute ?
btw, i used windows.attachEvent("onload", myMethod) instead of registering at the server side ...

greetings




function /*void*/ MultiListEnsureItemVisible(/*element*/ list)
{
if ( !list || !list.multiple || list.length == 0 ) return;

var lastItem = list[list.length-1];

if (lastItem.selected)
{
lastItem.selected = true;
return;
}
else
{
lastItem.selected = true;
lastItem.selected = false;
}

for (var i=0; i < list.length; i++)
{
if (list[i].selected) // or list.selected ?
{
list[i].selected = true;
return;
}
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum