ASP.NET: ListBox Scrollbar

Posted Tue, Jan 26 2010 19:46 by Deborah Kurata

This one is in the category of obvious once you know how to do it. But having done Silverlight of late, I could not recall how to turn on the scrollbar in a ASP.NET ListBox. There is no scrollbar property of any kind.

A bit of "guess and check" with Bing provided lots of custom control solutions, which were more than I wanted to do for one little list box.

So I finally just tried Intellisense to see what properties and methods were available. And there was a Rows property and it worked!

To turn on the scrollbar in a ListBox, just set the Rows property to the number of items to display. If  there are more than the defined number of items in the list, the scrollbar will automatically appear.

In HTML:

<asp:ListBox id="LB" runat="server"
    Rows="6">
    <asp:ListItem>Test1</asp:ListItem>
    <asp:ListItem>Test2</asp:ListItem>
    <asp:ListItem>Test3</asp:ListItem>
    <asp:ListItem>Test4</asp:ListItem>
    <asp:ListItem>Test5</asp:ListItem>
    <asp:ListItem>Test6</asp:ListItem>
    <asp:ListItem>Test7</asp:ListItem>
    <asp:ListItem>Test8</asp:ListItem>
</asp:ListBox>

The result appears like this:

image

The Listbox displays the first 6 items as per the Rows property. Since there are 8 items, it automatically displays the scrollbar.

Enjoy!

Filed under: , , , ,

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: