aspx页面 ASP.NET Web Forms中的DropDownList添加搜索功能使用select2

.NET兼职社区

select2依赖jquery

JS直接去官网下载:https://select2.org/getting-started/basic-usage或者https://www.bootcdn.cn/

csharp 复制代码
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <script src="Scripts/jquery-3.4.1.min.js"></script>
    <link href="Scripts/select2.min.css" rel="stylesheet" />
    <script src="Scripts/select2.min.js"></script>



      水果:  <asp:DropDownList ID="SelectSecondParty" runat="server" Width="300px"></asp:DropDownList>

    <script>
        $(document).ready(function () {
            $('#MainContent_SelectSecondParty').select2();
        });
    </script>

</asp:Content>

后台代码 初始化点数据:

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // 添加水果到DropDownList
            SelectSecondParty.Items.Add(new ListItem("苹果", "Apple"));
            SelectSecondParty.Items.Add(new ListItem("香蕉", "Banana"));
            SelectSecondParty.Items.Add(new ListItem("樱桃", "Cherry"));
            SelectSecondParty.Items.Add(new ListItem("葡萄", "Grape"));
            SelectSecondParty.Items.Add(new ListItem("柠檬", "Lemon"));
            SelectSecondParty.Items.Add(new ListItem("芒果", "Mango"));
            SelectSecondParty.Items.Add(new ListItem("橙子", "Orange"));
            SelectSecondParty.Items.Add(new ListItem("桃子", "Peach"));
            SelectSecondParty.Items.Add(new ListItem("梨", "Pear"));
            SelectSecondParty.Items.Add(new ListItem("菠萝", "Pineapple"));
            SelectSecondParty.Items.Add(new ListItem("草莓", "Strawberry"));
            SelectSecondParty.Items.Add(new ListItem("西瓜", "Watermelon"));
        }
    }
}

效果截图:

相关推荐
90后的晨仔几秒前
uni-app 安卓自有证书完全指南
前端
江湖十年4 分钟前
Go 虚荣域名详解:我用 go get -x 抓了个包,终于看清了 Go 工具链“对暗号”的全过程
后端·面试·go
Lost of 程序猿6 分钟前
船岸数据同步链路实战:SendFlag、单调版本号与断网三天后的续传
后端·c#·asp.net
卷无止境10 分钟前
大模型如何调用工具,一次讲清背后的技术门道
后端·python
90后的晨仔16 分钟前
Python 进阶:海象运算符与模式匹配,让你的代码更优雅!
后端
卷无止境6 小时前
智能体开发环境ADE浅析,编程工具的下一次范式跃迁
后端·python
码事漫谈9 小时前
DeepSeek 这波操作很凶
后端
CharlesYu0110 小时前
前端性能优化的第一性原理,是不断缩短“用户发起意图 → 获得可用结果”之间的时间
前端
Bs_MoneyMagnet10 小时前
基于springboot+vue的医疗健康便民服务平台的设计与实现 源码+文档
java·vue.js·spring boot·后端·spring