Listbox - how do I maintain scroll position?

I have a listbox web control that has about 100 items.
The user scrolls down to find an item he wants to select, clicks an ADD button to "move" it to another listbox that contains the items "selected" from the big list.
Clicking the ADD button causes a PostBack.

Scenario: User scrolls to item 50, about half way down the big list.  Selects an item and clicks ADD.  The item is moved correctly to the selected list, but the big list is reset to item #1, which is the top of the list.  I would like for the list to remain on position #50 after the postback.  

I've searched alot through Google, but I can only seem to find help on maintaining PAGE position.
Who is Participating?
Type25Connect With a Mentor Commented:
If you're simply transfering one item to another then why don't you use javascript?  Then there won't be any postback at all?

Otherwise, there are loads of ways of maintaining the state, using the viewstate object is probably best.

ie, in your listbox click event:

ViewState["selIndex"] = lstBox1.SelectedItem.Value;

Then in your page load

if (ViewState["selIndex"] != null)
     lstBox1.SelectedIndex = ViewState["selIndex"];

Try setting SmartNavigation = True in your forms. But i think this is ie specific. Anyways please give it a try.

dnowakAuthor Commented:
Nope.  Been there, done that.
It doesn't work anyhow - not even in IE6.
I need a real solution, one that will work on all browsers.
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Well, you can use a hidden text or somekind of object to store selectedindex once user selected a item in biglistbox.

Once page is postedback you check that hidden object if there is any selected index is avilable.

If soo then explicitly make biglistbox's current selectedindex to the one which is there in hidden object.

This should work fine for you...


Take help of predefined anchors.
Then you may jump/stay back at a specified position by mentioning anchor name in the url.
This definitely works on all browsers.
E.g., See the URLS how you receive in mails when  a new post is available in current thread of Expert Exchange.

To define an anchor -----> <a name="myanchor1"></a>
To jump to an anchor named "myanchor1"
-----> <a href="#myanchor1">Jump</a>
or http://URL_OF_THE_PAGE/PAGE_NAME#myanchor1
or <a href="#myanchor1">

Hope this gives an idea.
Incidently, if you mean physically scrolling the list to item 50 then i'm pretty sure it can't be done.

Let me have a quick look
Nope, don't think it can with a web control.
I'm currently using a great bit of help i got from here on IE6 (but i can't remember the PAQ#):

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender  
RegisterHiddenField("scrollLeft", Convert.ToString(Request.Form("scrollLeft")))  
RegisterHiddenField("scrollTop", Convert.ToString(Request.Form("scrollTop")))  
   Dim sb As New System.Text.StringBuilder(1000)  
   Dim nl As String = Environment.NewLine  
   With sb  
     .Append("<script language = ""javascript"">")  
   .Append(String.Concat(nl, "<!--", nl))  
     .Append("function SmartScroller_GetCoords() {")  
     .Append(String.Concat(nl, "var scrollX, scrollY;", nl))  
     .Append("if (document.all) {")  
     .Append(String.Concat(nl, "if (!document.documentElement.scrollLeft)", nl))  
   .Append("scrollX = document.body.scrollLeft;")  
     .Append(String.Concat(nl, "else", nl))  
     .Append("scrollX = document.documentElement.scrollLeft;")  
     .Append(String.Concat(nl, "if (!document.documentElement.scrollTop)", nl))  
     .Append("scrollY = document.body.scrollTop;")  
     .Append(String.Concat(nl, "else", nl))  
     .Append("scrollY = document.documentElement.scrollTop; }")  
     .Append(String.Concat(nl, "else {", nl))  
     .Append("scrollX = window.pageXOffset; scrollY = window.pageYOffset; }")  
     .Append(String.Concat(nl, nl))  
   .Append("document.getElementById('scrollLeft').value = scrollX;")  
    .Append("document.getElementById('scrollTop').value = scrollY;")  
    .Append(String.Concat(nl, "}", nl))      

     .Append(String.Concat(nl, "function SmartScroller_Scroll() {", nl))  
     .Append("var x = document.getElementById('scrollLeft').value;")  
     .Append(String.Concat(nl, "var y = document.getElementById('scrollTop').value;", nl))  
     .Append("window.scrollTo(x, y); }")  
     .Append(String.Concat(nl, nl, "window.onload = SmartScroller_Scroll;"))  
     .Append(String.Concat(nl, "window.onscroll = SmartScroller_GetCoords;", nl))  
     .Append("window.onclick = SmartScroller_GetCoords; window.onkeypress = SmartScroller_GetCoords;")  
     .Append(String.Concat(nl, "// -->", nl))  
   End With
   RegisterClientScriptBlock("scrollCode", sb.ToString)  
 End Sub  

'just insert that block into your page and when you postback, your screen position will stay the same.

hope this helps,
Guys,  i don't think he's talking about maining page state, i think he's talking about the actual list box shooting back up to the top.

That's what i got from it anyway????

sorry, when i say page state i meant page position
>> I've searched alot through Google, but I can only seem to find help on maintaining PAGE position.

See  :)
dnowakAuthor Commented:
I am resigning myself to the fact that javascript is the only way to do it.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.