zoukankan      html  css  js  c++  java
  • WB列表框知识点

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <asp:ListBox ID="ListBox1" runat="server" Height="187px" SelectionMode="Multiple" Width="141px"></asp:ListBox>
        
            <asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="TestDataContext" EntityTypeName="" TableName="Nation">
            </asp:LinqDataSource>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        
            <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="设置选中" />
        
        </div>
        </form>
    </body>
    </html>
    

      

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TestDataContext context = new TestDataContext();
                //用代码绑定数据
                ListBox1.DataSource = context.Nation;
                //显示哪一个字段
                ListBox1.DataTextField = "Name";
                ListBox1.DataValueField = "Code";
                //绑定
                ListBox1.DataBind();
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            //取选中项的值  只适用于单选
            //Label1.Text = ListBox1.SelectedValue.ToString();
            //多选
            //清值
            Label1.Text = "";
            foreach (ListItem item in ListBox1.Items)
            {
                if (item.Selected)
                {
                    Label1.Text += item.Value;//拼接字符串
                }
            }
           
        } 
        //设置哪像被选中
        protected void Button2_Click(object sender, EventArgs e)
        {
            foreach (ListItem item in ListBox1.Items)
            {
                if (item.Text=="汉族"|| item.Text=="满族")
                {
                    item.Selected = true;
                }
            }
        }
    }
    

      

  • 相关阅读:
    使用SignTool对软件安装包进行数字签名(二)--进行数字签名
    使用SignTool对软件安装包进行数字签名(一)--制作证书
    三角形相关算法--求解三角形顶点坐标
    子网掩码与子网个数、主机地址个数的关系
    pgsql中的lateral使用小结
    Git中rebase失败了如何进行恢复
    灰度发布
    go 中的WaitGroup
    pgsql中json格式数组查询结果变成了字符串
    Go中的unsafe
  • 原文地址:https://www.cnblogs.com/zhuxu/p/5052317.html
Copyright © 2011-2022 走看看