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

效果截图:

相关推荐
zwjapple4 小时前
docker-compose一键部署全栈项目。springboot后端,react前端
前端·spring boot·docker
tan180°6 小时前
MySQL表的操作(3)
linux·数据库·c++·vscode·后端·mysql
像风一样自由20206 小时前
HTML与JavaScript:构建动态交互式Web页面的基石
前端·javascript·html
aiprtem7 小时前
基于Flutter的web登录设计
前端·flutter
浪裡遊7 小时前
React Hooks全面解析:从基础到高级的实用指南
开发语言·前端·javascript·react.js·node.js·ecmascript·php
优创学社27 小时前
基于springboot的社区生鲜团购系统
java·spring boot·后端
why技术7 小时前
Stack Overflow,轰然倒下!
前端·人工智能·后端
幽络源小助理7 小时前
SpringBoot基于Mysql的商业辅助决策系统设计与实现
java·vue.js·spring boot·后端·mysql·spring
GISer_Jing7 小时前
0704-0706上海,又聚上了
前端·新浪微博
止观止7 小时前
深入探索 pnpm:高效磁盘利用与灵活的包管理解决方案
前端·pnpm·前端工程化·包管理器