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"));
        }
    }
}

效果截图:

相关推荐
universe_014 分钟前
day25|学习前端js
前端·笔记
MrSYJ8 分钟前
UserDetailService是在什么环节生效的,为什么自定义之后就能被识别
java·spring boot·后端
张志鹏PHP全栈9 分钟前
Rust第一天,安装Visual Studio 2022并下载汉化包
后端
Zuckjet9 分钟前
V8 引擎的性能魔法:JSON 序列化的 2 倍速度提升之路
前端·chrome·v8
MrSkye9 分钟前
🔥React 新手必看!useRef 竟然不能触发 onChange?原来是这个原因!
前端·react.js·面试
estarlee15 分钟前
公交线路规划免费API接口详解
后端
wayman_he_何大民16 分钟前
初识机器学习算法 - AUM时间序列分析
前端·人工智能
juejin_cn17 分钟前
前端使用模糊搜索fuse.js和拼音搜索pinyin-match提升搜索体验
前端
无责任此方_修行中28 分钟前
从 HTTP 轮询到 MQTT:我们在 AWS IoT Core 上的架构演进与实战复盘
后端·架构·aws
考虑考虑34 分钟前
postgressql更新时间
数据库·后端·postgresql