zoukankan      html  css  js  c++  java
  • ListBox控件例子

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ListBox.aspx.cs" Inherits="WebApplication1.ListBox" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:ListBox ID="listUsers" runat="server" SelectionMode="Multiple"></asp:ListBox>
        <asp:Button  ID="btnOK" runat="server" Text="确定" onclick="btnOK_Click" />
        </div>
        </form>
    </body>
    </html>

    CS:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Data;
    
    namespace WebApplication1
    {
        public partial class ListBox : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    BindUserList();
                }
            }
            private void BindUserList()
            {
                SqlConnection conn = new SqlConnection(@"server=Rose-PCSQLEXPRESS;Database=User;User Id=sa;password=");
                SqlCommand command = new SqlCommand("Select ID,RealName from UserInfo", conn);
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataTable data = new DataTable();
                adapter.Fill(data);
    
                listUsers.DataTextField = "RealName";
                listUsers.DataValueField = "ID";
                listUsers.DataSource = data;
                listUsers.DataBind();
            }
    
            protected void btnOK_Click(object sender, EventArgs e)
            {
                string selectedUserName = string.Empty;
                //遍历ListBox中的每一个选项
                foreach (ListItem item in listUsers.Items)
                {
                    //如果项被选中
                    if (item.Selected)
                    {
                        selectedUserName += item.Value + ",";
                    }
                }
                //至少有一项被选中
                if (!string.IsNullOrEmpty(selectedUserName))
                {
                    //删除最后一个“,”符号
                    selectedUserName = selectedUserName.Remove(selectedUserName.Length - 1);
                }
                Response.Write("你选择的用户编号有:"+selectedUserName);
            
            }
        }
    }
  • 相关阅读:
    金融大数据行业应用及发展全洞察
    金融大数据行业应用及发展全洞察
    R语言-组间差异的非参数检验
    R语言-组间差异的非参数检验
    互联网,将从内部颠覆企业管理模式
    GitHub使用教程
    sublime text3编辑器经常使用快捷方式
    webpy学习笔记之中的一个
    浏览器的重绘和重排的影响
    《Java并发编程实战》第九章 图形用户界面应用程序界面 读书笔记
  • 原文地址:https://www.cnblogs.com/ai394495243/p/3355864.html
Copyright © 2011-2022 走看看