简体   繁体   中英

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() {
  ...?
}

尝试:

Listbox.rows = Listbox.Items.count

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM