2

How can I add horizontal scroll capabilities to the asp.net listbox control?

4

4 回答 4

3

Example to add horizontal scroll:

<asp:ListBox ID="List" runat="server" Height="320px" Width="100%" style="overflow-x:auto;"SelectionMode="Multiple">
</asp:ListBox>

CSS3 overflow-x Property: http://www.w3schools.com/cssref/css3_pr_overflow-x.asp

于 2015-04-08T19:10:49.313 回答
1

If you really, really need it, one idea would be to create a custom ListBox class whose HTML looks like this: sets the width of the SELECT to that of your widest value's width (the max width of the scrollbar, for example). Now wrap that SELECT inside of a DIV of the 'constrained' size and let it scroll on overflow.

Here's a quick example starting down those lines, here's the type of HTML you want spit out by a control:

<div style="width:200px; height:100px; overflow:auto;">
<SELECT size="4">
<OPTION
Value="1">blahblahblahblahblahblahblahblahblahblah blahblah</OPTION>
<OPTION Value="2">2</OPTION>
<OPTION Value="3">3</OPTION>
<OPTION Value="4">4</OPTION>
</SELECT>
</div>

so in essence I'd recommend creating a composite custom control for this, which renders this HTML. They're pretty easy to make, Google on the terms 'composite control asp.net'.

The toughest part will be matching up the div dimensions to that of the select box, to make the scrollbars work/line up properly. That's why it's kinda tricky.

Source

Also, take a look at this: Automatically Adding/Hide Horizontal Scroll bar in the ListBox control

EDIT: Make sure you have enough height to include the scroll bar height or else you'll get the vertical scroll bar on both controls.

于 2008-09-05T11:12:34.243 回答
1

We can put this list box inside a DIV and set the style for DIV to overflow which will automatically show the scroll bar whenever necessary.

Your aspx page has the following DIV:

<div id='hello' style="Z-INDEX: 102; LEFT: 13px; OVERFLOW: 
            auto; WIDTH: 247px; POSITION: absolute; TOP: 62px; HEIGHT: 134px" >

Put your asp:listbox inside the DIV definition. In page_load function, you need to define the width and height of the list box properly, so that it won't overflow with the DIV.

private void Page_Load(object sender, System.EventArgs e)
{
    if (!IsPostBack)
    {

        int nItem = Convert.ToInt32(ListBox1.Items.Count * 17);
        ListBox1.Height = nItem; 

        ListBox1.Width = 800; 

    }
}

Code and solution available at http://www.codeproject.com/KB/custom-controls/HorizontalListBox.aspx

于 2008-09-05T11:16:02.597 回答
0

如果您只是为了显示目的而这样做,您可以通过使用具有多行属性的文本框以另一种方式进行。通过这样附加新行的文本!

    List<Yourclass> result = null;
    result = Objname.getResult(Parameter1, Parameter2);
    foreach (Yourclass res in result)
    {
        txtBoxUser.Text += res.Fieldname1.ToString();
        txtBoxUser.Text += "\r\n" + res.Fieldname2.ToString();
        txtBoxUser.Text += "\n\n";
    }

因此,您将获得多行文本框的视图,其中所有数据都以良好的格式排列,如上面的代码(新行和所有)。如果它超出了文本框的宽度,它也会包裹你的文本。此外,您无需担心滚动条,在这里您只会看到垂直滚动条,因为我们所有的结果都已根据文本框的行为进行包装。

于 2015-06-26T14:08:14.247 回答