Asp. NET, how to use the ListBox list box control _ Basic Application

Source: Internet
Author: User

The ListBox control allows a user to select one or more items from a predefined list. It differs from the DropDownList control in that it not only displays multiple items at once, but optionally allows the user to select more than one item at a time.

One, property

Property value function
SelectionMode single| Multiple List selection mode: Radio | multiple selection
Selected false| True is a selected state

Second, the example

Listbox.aspx

Copy Code code as follows:

<%@ Page language= "C #" autoeventwireup= "true" codefile= "ListBox.aspx.cs" inherits= "Webcontrols_listbox"%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<title></title>
<style type= "Text/css" >
. style1
{
width:293px;
}
. style2
{
width:233px;
}
</style>
<body>
<form id= "Form1" runat= "Server" >
<div>
Province: <asp:listbox id= "Lstprov" runat= "Server" >
<asp:ListItem> Shandong </asp:ListItem>
<asp:listitem selected= "True" > Hebei </asp:ListItem>
<asp:ListItem> Inner Mongolia </asp:ListItem>
<asp:ListItem> Anhui </asp:ListItem>
</asp:ListBox>
<br/>
<asp:button id= "btnsubmit" runat= "Server" text= "Submit" onclick= "btnSubmit_Click"/>
Your choice is: <asp:label id= "lblstate" runat= "Server" ></asp:Label>

2, multiple-selection list box Province: <asp:listbox id= "LSTPROV2" runat= "Server" selectionmode= "multiple" >
<asp:ListItem> Shandong </asp:ListItem>
<asp:listitem selected= "True" > Hebei </asp:ListItem>
<asp:ListItem> Inner Mongolia </asp:ListItem>
<asp:ListItem> Anhui </asp:ListItem>
</asp:ListBox>
Tip: Ctrl + click <br/>
<asp:button id= "BtnSubmit2" runat= "Server" text= "Submit"
onclick= "Btnsubmit2_click"/>
Your choice is: <asp:label id= "LblState2" runat= "Server" ></asp:Label>
</div>
</form>
</body>

ListBox.aspx.cs

Copy Code code as follows:

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); Remove the last ","
Lblstate2.text = str;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.