Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get first selected item in an asp:ListBox with SelectionMode="Multiple" to appear at top?
    text
    copied!<p>In asp.net if you define an asp:ListBox as follows:</p> <pre><code>&lt;asp:ListBox ID="listBox2" runat="server" SelectionMode="Single" Rows="3"&gt; &lt;asp:ListItem&gt;1&lt;/asp:ListItem&gt; &lt;asp:ListItem&gt;2&lt;/asp:ListItem&gt; &lt;asp:ListItem&gt;3&lt;/asp:ListItem&gt; &lt;asp:ListItem&gt;4&lt;/asp:ListItem&gt; &lt;asp:ListItem&gt;5&lt;/asp:ListItem&gt; &lt;asp:ListItem&gt;6&lt;/asp:ListItem&gt; &lt;asp:ListItem&gt;7&lt;/asp:ListItem&gt; &lt;asp:ListItem Selected="True"&gt;8&lt;/asp:ListItem&gt; &lt;asp:ListItem&gt;9&lt;/asp:ListItem&gt; &lt;asp:ListItem&gt;10&lt;/asp:ListItem&gt; &lt;/asp:ListBox&gt; </code></pre> <p>You will see that the selected item is visible at the top. But if you define it as a multiple selection list box, the selected items are not visible, and you have to scroll down the list to find them.</p> <pre><code>&lt;asp:ListBox ID="listBox1" runat="server" SelectionMode="Multiple" Rows="3"&gt; &lt;asp:ListItem&gt;1&lt;/asp:ListItem&gt; &lt;asp:ListItem&gt;2&lt;/asp:ListItem&gt; &lt;asp:ListItem&gt;3&lt;/asp:ListItem&gt; &lt;asp:ListItem&gt;4&lt;/asp:ListItem&gt; &lt;asp:ListItem&gt;5&lt;/asp:ListItem&gt; &lt;asp:ListItem&gt;6&lt;/asp:ListItem&gt; &lt;asp:ListItem&gt;7&lt;/asp:ListItem&gt; &lt;asp:ListItem Selected="True"&gt;8&lt;/asp:ListItem&gt; &lt;asp:ListItem Selected="True"&gt;9&lt;/asp:ListItem&gt; &lt;asp:ListItem&gt;10&lt;/asp:ListItem&gt; &lt;/asp:ListBox&gt; </code></pre> <p>I've done a bit of google searching, and see that this is not an uncommon problem. But I did not find any good solutions, so thought I would ask here.</p> <p>My first thought was to try a bit of JQuery to solve this. What are some of your solutions?</p> <p>Several of the answers don't even understand my problem. I only care about ensuring that the first selected option is visible. Make sure it is scrolled into view.</p> <p>I played around with JQuery, and came up with the following:</p> <pre><code>&lt;script type="text/javascript"&gt; $(document).ready(function() { $("#listBox1 option:nth-child(8)").attr('selected',true); }); &lt;/script&gt; </code></pre> <p>But I think I like @Cerebrus's answer the best.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload