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 小时前
虚拟列表兼容老DOM操作
前端
悦悦子a啊5 小时前
Python之--基本知识
开发语言·前端·python
Piper蛋窝5 小时前
深入 Go 语言垃圾回收:从原理到内建类型 Slice、Map 的陷阱以及为何需要 strings.Builder
后端·go
安全系统学习6 小时前
系统安全之大模型案例分析
前端·安全·web安全·网络安全·xss
涛哥码咖6 小时前
chrome安装AXURE插件后无效
前端·chrome·axure
OEC小胖胖6 小时前
告别 undefined is not a function:TypeScript 前端开发优势与实践指南
前端·javascript·typescript·web
行云&流水7 小时前
Vue3 Lifecycle Hooks
前端·javascript·vue.js
Sally璐璐7 小时前
零基础学HTML和CSS:网页设计入门
前端·css
老虎06277 小时前
JavaWeb(苍穹外卖)--学习笔记04(前端:HTML,CSS,JavaScript)
前端·javascript·css·笔记·学习·html
灿灿121387 小时前
CSS 文字浮雕效果:巧用 text-shadow 实现 3D 立体文字
前端·css