ASP.NET ListBox, post back and focus of selected items

  • Thread starter Thread starter probashi
  • Start date Start date
P

probashi

Hi,

Issue: After post back selected item of a list box is getting out of
focus (when it contains more items than it's size).

I have a List Box in an ASPX page. I select an item from the ListBox
and after post back the item is still selected but it is not focused
(i.e. I have to scroll down to see the selected item)

Any way to fix this!

Thanks.
 
Unfortunately it does not help.

Looks like custom JavaScript will be my last option.

Thanks
 
probashi,

on page load event you could do something like the following to ensure the
selected item is visible:
<script type="text/javascript">
var lst = document.getElementById('<%= myListBox.ClientID %>';
if (lst && lst.selectedIndex > -1){
lst.focus();
lst.selectedIndex = lst.selectedIndex;
}
</script>

Hope it is not too late for you
 
Looks like I cannot solve this with JavaScript either.

Here is the code I tried.


<script type= "text/javascript">
var lst = document.getElementById("list1");
var options = lst.options;
var option;
var i;
for(i = 0; i < options.length; i++)
{

if(options.selected)
{

options.focus();
}
}
</script>
 
probashi,

take a look at my previous response - I have tested it before posting - it
does select the correct item on postback - you need to focus the list and
then select the selected item again.

probashi said:
Looks like I cannot solve this with JavaScript either.

Here is the code I tried.


<script type= "text/javascript">
var lst = document.getElementById("list1");
var options = lst.options;
var option;
var i;
for(i = 0; i < options.length; i++)
{

if(options.selected)
{

options.focus();
}
}
</script>

Unfortunately it does not help.

Looks like custom JavaScript will be my last option.

Thanks









- Show quoted text -
 
I am sorry for not describing the issue properly.

This issue exists only for multi-select list box.

The code you sent does not work for multi-select list box. It makes
the selected item visible but wipes out all the other selections.

Thanks.
 
Back
Top