ASP.NET中 ListBox列表框控件的使用方法
ListBox 控件允許用戶從預(yù)定義的列表中選擇一個或多個項。它與 DropDownList 控件的不同之處在于,它不但可以一次顯示多個項,而且(可選)還允許用戶選擇多個項。
一、屬性
| 屬性 | 值 | 作用 |
|---|---|---|
| SelectionMode | Single|Multiple | 列表選擇模式:單選|多選 |
| Selected | False|True | 是否為選中狀態(tài) |
二、示例
ListBox.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ListBox.aspx.cs" Inherits="WebControls_ListBox" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
<html xmlns=" <head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 293px;
}
.style2
{
width: 233px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<h5>1,單選列表框</h5>
省份:<asp:ListBox ID="lstProv" runat="server">
<asp:ListItem>山東</asp:ListItem>
<asp:ListItem Selected="True">河北</asp:ListItem>
<asp:ListItem>內(nèi)蒙</asp:ListItem>
<asp:ListItem>安徽</asp:ListItem>
</asp:ListBox>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="提交" onclick="btnSubmit_Click" />
<hr />
你選擇的為:<asp:Label ID="lblState" runat="server"></asp:Label>
<hr />
<h5>
2,多選列表框</h5>
省份:<asp:ListBox ID="lstProv2" runat="server" SelectionMode="Multiple">
<asp:ListItem>山東</asp:ListItem>
<asp:ListItem Selected="True">河北</asp:ListItem>
<asp:ListItem>內(nèi)蒙</asp:ListItem>
<asp:ListItem>安徽</asp:ListItem>
</asp:ListBox>
提示:Ctrl+單擊<br />
<asp:Button ID="btnSubmit2" runat="server" Text="提交"
onclick="btnSubmit2_Click" />
<hr />
你選擇的為:<asp:Label ID="lblState2" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
ListBox.aspx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class WebControls_ListBox : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
lblState.Text = lstProv.SelectedValue;
}
protected void btnSubmit2_Click(object sender, EventArgs e)
{
string str = string.Empty;
for (int i = 0; i < lstProv2.Items.Count; i++)
{
if (lstProv2.Items[i].Selected == true)
{
str = string.Format("{0}{1},",str,lstProv2.Items[i].Value);
}
}
str = str.Substring(0, str.Length - 1); //去掉最后一個“,”
lblState2.Text = str;
}
}
- asp.net mvc下拉框Html.DropDownList 和DropDownListFor的常用方法
- ASP.NET MVC DropDownList數(shù)據(jù)綁定及使用詳解
- ASP.NET Ajax級聯(lián)DropDownList實現(xiàn)代碼
- asp.net省市三級聯(lián)動的DropDownList+Ajax的三種框架(aspnet/Jquery/ExtJs)示例
- (asp.net c#)DropDownList綁定后顯示對應(yīng)的項的兩種方法
- 打造基于jQuery的高性能TreeView(asp.net)
- 關(guān)于ASP.NET中TreeView用法的一個小例子
- ASP.NET實現(xiàn)TreeView的XML數(shù)據(jù)源綁定實例代碼
- ASP.NET使用TreeView顯示文件的方法
- ASP.NET中使用TreeView顯示文件的方法
- ASP.NET中DropDownList和ListBox實現(xiàn)兩級聯(lián)動功能
- asp.net中ListBox 綁定多個選項為選中及刪除實現(xiàn)方法
- Asp.net treeview實現(xiàn)無限級樹實現(xiàn)代碼
- asp.net實現(xiàn)DropDownList,TreeView,ListBox的無限極分類目錄樹
相關(guān)文章
ASP.NET 5已終結(jié),迎來ASP.NET Core 1.0和.NET Core 1.0
命名是非常困難的事情,微軟這次為了和ASP.NET4.6做區(qū)分,采用了全新的命名方式ASP.NET Core 1.0,它是一個全新的框架。2016-03-03
SQL Server 2005 RTM 安裝錯誤 :The SQL Server System Configuratio
SQL Server 2005 RTM 安裝錯誤 :The SQL Server System Configuration Checker cannot be executed due to...2007-02-02
ASP.NET?Core中的Configuration配置一
這篇文章介紹了ASP.NET?Core中的Configuration配置,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
ASP.NET實現(xiàn)word文檔在線預(yù)覽功能代碼
文檔管理系統(tǒng)需要實現(xiàn)WORD能在線預(yù)覽功能,以前覺得挺難的,經(jīng)過一番研究發(fā)現(xiàn),WORD自帶的另存為可以保存為HTMl文件。2010-07-07
詳解ABP框架中領(lǐng)域?qū)拥念I(lǐng)域事件Domain events
ABP是基于ASP.NET框架之上的Web開發(fā)框架(GitHub:https://github.com/aspnetboilerplate),這篇我們來詳解ABP框架中領(lǐng)域?qū)拥念I(lǐng)域事件Domain events,需要的朋友可以參考下2016-06-06
詳解Asp.net 5中的ApplicationBuilder
這篇文章介紹了Asp.net 5中的ApplicationBuilder,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-01-01
asp.net FindControl方法誤區(qū)和解析
在ASP.NET中Control都有一個FindControl方法,其作用是根據(jù)ID(注意既不是UniqueID也不是ClientID)在Control所在的命名容器中尋找相應(yīng)控件,但實際使用中存在很多誤區(qū)和陷阱,下面談?wù)剛€人對此的理解2012-01-01
創(chuàng)建ASP.NET?Core?Web應(yīng)用程序并介紹項目模板
這篇文章介紹了創(chuàng)建ASP.NET?Core?Web應(yīng)用程序的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02

