Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

January 26, 2010

ASP.NET: ListBox Scrollbar

Filed under: ASP.NET,C#,VB.NET @ 9:46 pm

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!

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2019 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs