[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3410
  • Last Modified:

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.
0
dnowak
Asked:
dnowak
1 Solution
 
surajgupthaCommented:
Try setting SmartNavigation = True in your forms. But i think this is ie specific. Anyways please give it a try.

Regards,
Suraj
0
 
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.
0
 
sachiekCommented:
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...

Sachi
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sriggummaCommented:
dnowak,

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.
0
 
Type25Commented:
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;
lstBox2.Items.Add(lsltBox1.SelectedItem.Text);

Then in your page load

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

0
 
Type25Commented:
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
0
 
Type25Commented:
Nope, don't think it can with a web control.
0
 
mcgantsCommented:
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(nl)  
    .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))  
     .Append("</script>")  
   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,
mcg
0
 
Type25Commented:
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????

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

See  :)
0
 
dnowakAuthor Commented:
I am resigning myself to the fact that javascript is the only way to do it.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now