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

效果截图:

相关推荐
利刃大大5 分钟前
【Vue】props完整语法 && 非父子组件通信 && provide && inject && eventBus
前端·javascript·vue.js
萧曵 丶8 分钟前
前端工程化项目中全类型配置文件的详细解析
前端·javascript·配置文件·工程化
讯方洋哥14 分钟前
HarmonyOS App开发——鸿蒙公共事件App开发
服务器·前端·javascript
短剑重铸之日18 分钟前
《SpringCloud实用版》SpringCloud Alibaba和SpringCloud的区别
后端·spring·spring cloud·sca
Можно18 分钟前
吃透 Vue 的 v - 指令家族:从入门到精通
前端·javascript·vue.js
光影少年29 分钟前
react的hooks优缺点、底层实现及hooks参数
前端·react.js·前端框架
weixin_4569074141 分钟前
2026+:html+css 生态的成型之年与平台化跃迁
前端·css·html
上海合宙LuatOS43 分钟前
LuatOS框架的使用(2)
java·服务器·开发语言·前端·数据库·嵌入式硬件·php
码农水水1 小时前
SpringBoot配置优化:Tomcat+数据库+缓存+日志全场景教程
java·数据库·spring boot·后端·算法·tomcat·哈希算法
江湖有缘1 小时前
Docker部署NextTrace Web路由工具
前端·docker·容器