Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set width and height of ASP.NET (4.0) listbox to fit the items

I have a listbox on a page that is bound to a linqdatasource. In the ondatabound event I am trying to make the size of the listbox on screen fit the items, to avoid having to scroll and to avoid excessive white space.

How can I do this?

protected void ListBox1_DataBound(object sender, EventArgs e)
{
  ListBox1.FitToItems();
}
protected void FitToItems() {
  ...?
}
like image 259
tomsv Avatar asked Oct 07 '11 07:10

tomsv


2 Answers

Try:

Listbox.rows = Listbox.Items.count
like image 119
KaRty Avatar answered Oct 06 '22 00:10

KaRty


Try setting the size attribute on the generated tag. Something like...

$(document).ready(function() {
  $('#<%=this.ListBox1.ClientID%>').attr('size', $('#<%=this.ListBox1.ClientID%> option').length);
});

using jQuery.

Or set it using ListBox.Rows = ListBox.Items.Count server side (not tried this myself, but I think that's what it does).

like image 37
Balthy Avatar answered Oct 06 '22 00:10

Balthy