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

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.
 
P

probashi

Unfortunately it does not help.

Looks like custom JavaScript will be my last option.

Thanks
 
G

Guest

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
 
P

probashi

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>
 
G

Guest

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 -
 
P

probashi

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top