• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2060
  • Last Modified:

ListBox scrolls to top on postback

Asp ListBox set to MultiSelect and inside an update panel.  When the postback occurs it always moves the ListBox's current position to the top.

I can't find anything in the Framework to offset this behavior.  I cannot seem to get a JavaScript event to fire after the postback (I can get one to fire before in plenty of locations).  I've tried Page.ClientScript.RegisterStartupScript() &  Page.ClientScript.RegisterOnSubmitStatement.  I've tried putting it in the onload of the page, but it doesn't fire when the form posts back.  I have tried Page.MaintainScrollPositionOnPostback and even the obsolete SmartNavigation tag.

I've google and found people with a similiar problem but none of the solutions work.  Somebody has had to have put a list box in an update panel and had this before... right? :P  I thought about using the window.onload = function() but it doesn't load whenever a postback occurs with Ajax.  
0
IUFITS
Asked:
IUFITS
  • 2
1 Solution
 
anna_arunCommented:
If you are populating your ListBox in PageLoad, then check for POstBack
for ex.
Page_Load()
{
  if(!IsPostBack)
  {
     //Put your code of populating list box here
  }
}

It mightbe happening if the code is outside !IsPOstBack block that it is repopulating the listbox in teh case of Postback.

Try it out
0
 
IUFITSAuthor Commented:
That was a great thought.  I am loading the list box from a database in Page_Load but the code is already in a !IsPostBack if statement.
0
 
IUFITSAuthor Commented:
I figured out a solution, this was 5 hours I'll never get back.    ;)  The below code snippet needs to go in the Page_Load but needs to run everytime, so don't nest it in a IsPostBack block.  
        ScriptManager.RegisterClientScriptBlock(Page, btnAdd.GetType, "Reposition", "if (document.getElementById('lstPrograms')) { document.getElementById('lstPrograms').selectedIndex=" & lstPrograms.SelectedIndex & "; }", True)

Open in new window

0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

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