ListBox.SelectionMode Egenskap
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Hämtar eller anger kontrollens markeringsläge ListBox .
public:
virtual property System::Web::UI::WebControls::ListSelectionMode SelectionMode { System::Web::UI::WebControls::ListSelectionMode get(); void set(System::Web::UI::WebControls::ListSelectionMode value); };
public virtual System.Web.UI.WebControls.ListSelectionMode SelectionMode { get; set; }
member this.SelectionMode : System.Web.UI.WebControls.ListSelectionMode with get, set
Public Overridable Property SelectionMode As ListSelectionMode
Egenskapsvärde
Ett av ListSelectionMode värdena. Standardvärdet är Single.
Undantag
Det angivna markeringsläget är inte ett av ListSelectionMode värdena.
Exempel
I följande exempel visas hur du använder SelectionMode egenskapen så att användaren kan välja flera val från ListBox kontrollen.
<%@ Page Language="C#" AutoEventWireup="True" %>
<!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>ListBox Example</title>
<script runat="server">
void SubmitBtn_Click(Object sender, EventArgs e)
{
Message.Text = "You chose: <br />";
// Iterate through the Items collection of the ListBox and
// display the selected items.
foreach (ListItem item in ListBox1.Items)
{
if(item.Selected)
{
Message.Text += item.Text + "<br />";
}
}
}
</script>
</head>
<body>
<h3>ListBox Example</h3>
<form id="form1" runat="server">
Select items from the list and click Submit. <br />
<asp:ListBox id="ListBox1"
Rows="6"
Width="100px"
SelectionMode="Multiple"
runat="server">
<asp:ListItem Selected="True">Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
<asp:ListItem>Item 4</asp:ListItem>
<asp:ListItem>Item 5</asp:ListItem>
<asp:ListItem>Item 6</asp:ListItem>
</asp:ListBox>
<br /><br />
<asp:button id="Button1"
Text="Submit"
OnClick="SubmitBtn_Click"
runat="server" />
<br /><br />
<asp:Label id="Message"
runat="server"/>
</form>
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>
<!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>ListBox Example</title>
<script runat="server">
Sub SubmitBtn_Click(sender As Object, e As EventArgs)
Message.Text = "You chose: <br />"
' Iterate through the Items collection of the ListBox and
' display the selected items.
Dim item As ListItem
For Each item in ListBox1.Items
If item.Selected Then
Message.Text &= item.Text & "<br />"
End If
Next
End Sub
</script>
</head>
<body>
<h3>ListBox Example</h3>
<form id="form1" runat="server">
Select items from the list and click Submit. <br />
<asp:ListBox id="ListBox1"
Rows="6"
Width="100px"
SelectionMode="Multiple"
runat="server">
<asp:ListItem Selected="True">Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
<asp:ListItem>Item 4</asp:ListItem>
<asp:ListItem>Item 5</asp:ListItem>
<asp:ListItem>Item 6</asp:ListItem>
</asp:ListBox>
<br /><br />
<asp:button id="Button1"
Text="Submit"
OnClick="SubmitBtn_Click"
runat="server" />
<br /><br />
<asp:Label id="Message"
runat="server"/>
</form>
</body>
</html>
Kommentarer
Använd egenskapen SelectionMode för att ange kontrollens funktionssätt ListBox . Om du anger den här egenskapen till anger du att ListSelectionMode.Single endast ett enskilt objekt kan väljas från ListBox kontrollen, medan ListSelectionMode.Multiple anger att flera objekt kan väljas.
Värdet för egenskapen SelectionMode lagras i visningstillstånd.