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.  
LVL 3
IUFITSAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.