【问题标题】:Asp.Net Dropdownlist Set Item View limitAsp.Net Dropdownlist 设置项目查看限制
【发布时间】:2016-11-02 03:35:42
【问题描述】:

我在 Asp.Net 页面中有一个国家 DropDownList。我从数据库中绑定 DropDownList。它给了我 239 项。而且页面滚动很大。

所以,我的问题是如何在下拉列表中设置 10 项,然后在列表中滚动。

<asp:DropDownList ID="ddlcountry" AutoPostBack="true" AppendDataBoundItems="true"
     runat="server" OnSelectedIndexChanged="ddlcountry_SelectedIndexChanged">
</asp:DropDownList>

【问题讨论】:

  • 你可以试试 DropDownList1.Attributes.Add("Size", "10");或者为什么不使用 aspnet ListBox 代替?当项目大于列表大小时,ListBox 提供自动滚动条

标签: c# asp.net drop-down-menu asp.net-controls


【解决方案1】:

您可以通过两种方式进行设置。

一个是,

    <asp:DropDownList ID="DropDownList1" runat="server" Height="50px" 
Style="position: static"> </asp:DropDownList>

<asp:DropDownList ID="DropDownList1" size="10" runat="server"></asp:DropDownList> 

【讨论】:

    【解决方案2】:

    你可以使用Size属性

    <asp:DropDownList Size="10" ID="ddlcountry" AutoPostBack="true"  AppendDataBoundItems="true"
     runat="server"  OnSelectedIndexChanged="ddlcountry_SelectedIndexChanged"/>
    

    【讨论】:

    • 感谢您的快速响应..但尺寸属性不起作用。
    【解决方案3】:

    为什么要使用滚动使用 select2 搜索插件。

    Select2 Example

    1.在你的.aspx文件中引用select2.css和select2.js

    2.

     <asp:DropDownList ID="ddlProductSubType" runat="server" ClientIDMode="Static" AppendDataBoundItems="True">
                                            <asp:ListItem Value="">Select</asp:ListItem>
    
                                        </asp:DropDownList>
    

    3.

      <script>
          $(function() {
              $('#ddlProductSubType').select2({
                  placeholder: 'select a state',
                  //tags: "true",
                  //allowClear: true,
                  theme: "classic"
              });
    });
      </script>
    

    注意,我已经使用过它,它可以很好地响应多达 3,000 个下拉列表项。如果您想过滤/按需加载,select2 文档中有选项

    【讨论】:

      【解决方案4】:

      其实这是一个非常有趣和棘手的任务,我们需要为此做一些配置并应用一些 css

      由于 DropDownList 没有内置滚动条,所以我们需要使用 OnMouseDown, OnFocusOut,OnDblClick 属性手动完成

      <asp:DropDownList ID="ddlcountry" AutoPostBack="true" AppendDataBoundItems="true"
           runat="server" 
           CssClass="DDlCountry"  
           OnMouseDown="this.size=5;" 
           OnFocusOut="this.size=1;" 
           OnDblClick="this.size=1;">
      </asp:DropDownList> 
      

      为此 DDlCountry 添加新的 CSS 类

      .DDlCountry {
              width: 128px;
              margin: 0 15px 0 0;
              font: 12px tahoma;
              max-height: 200px;
              overflow-y: scroll;
              position: relative;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-17
        • 2017-10-23
        • 1970-01-01
        • 1970-01-01
        • 2013-08-11
        相关资源
        最近更新 更多