HtmlSelect.Multiple 属性

定义

获取或设置一个值,该值指示是否可以在 HtmlSelect 控件中同时选择多个项。

public:
 property bool Multiple { bool get(); void set(bool value); };
public bool Multiple { get; set; }
member this.Multiple : bool with get, set
Public Property Multiple As Boolean

属性值

如果可以在 HtmlSelect 控件中同时选择多个项,则为 true;否则,为 false。 默认值是 false

示例

下面的代码示例演示如何使用 Multiple 属性指定是否可以在 控件中选择 HtmlSelect 多个项。

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

      void Button_Click (Object sender, EventArgs e)
      {

         Label1.Text = "You selected:";

         for (int i=0; i<=Select1.Items.Count - 1; i++)
         {
  
            if (Select1.Items[i].Selected)
               Label1.Text += "<br />    -" + Select1.Items[i].Text;      

         }

         Select1.Size = Convert.ToInt32(Select2.Value);

      }

      void Check_Changed (Object sender, EventArgs e)
      {
        
         Select1.Multiple = CheckBox1.Checked;

      }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title> HtmlSelect Example </title>
</head>
<body>
<form id="form1" runat="server">
   <div>

      <h3> HtmlSelect Example </h3>

      Select item(s) from the list: <br /><br />

      <select id="Select1" 
              multiple="true"
              runat="server">

         <option value="1" selected="selected"> Item 1 </option>
         <option value="2"> Item 2 </option>
         <option value="3"> Item 3 </option>
         <option value="4"> Item 4 </option>
         <option value="5"> Item 5 </option>
         <option value="6"> Item 6 </option>

      </select>

      <hr />

      HtmlSelect Size: <br />

      <select id="Select2" 
              runat="server">

         <option value="1"> 1 </option>
         <option value="2"> 2 </option>
         <option value="3"> 3 </option>
         <option value="4" selected="selected"> 4 </option>
         <option value="5"> 5 </option>
         <option value="6"> 6 </option>

      </select>

        

      <asp:CheckBox id="CheckBox1"
           Text="Enable Multiple Property"
           AutoPostBack="True"
           OnCheckedChanged="Check_Changed"
           Checked="True"
           runat="server"/>

      <br /><br />

      <button id="Button1"
              onserverclick="Button_Click"
              runat="server">

         Submit

      </button>

      <br /><br />

      <asp:Label id="Label1" runat="server"/>

   </div>
</form>
</body>
</html>
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    Sub Button_Click (sender As Object, e As EventArgs)
        Dim i As Integer

        Label1.Text = "You selected:"
        For i = 0 to Select1.Items.Count - 1
           If Select1.Items(i).Selected Then
               Label1.Text = Label1.Text & "<br />    -" & Select1.Items(i).Text
           End If         
        Next

        Select1.Size = CInt(Select2.Value)

      End Sub

      Sub Check_Changed (sender As Object, e As EventArgs)
         Select1.Multiple = CheckBox1.Checked
      End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title> HtmlSelect Example </title>
</head>
<body>
<form id="form1" runat="server">
   <div>

      <h3> HtmlSelect Example </h3>

      Select item(s) from the list: <br /><br />

      <select id="Select1" 
              multiple="true"
              runat="server">

         <option value="1"> Item 1 </option>
         <option value="2"> Item 2 </option>
         <option value="3"> Item 3 </option>
         <option value="4" selected="selected"> Item 4 </option>
         <option value="5"> Item 5 </option>
         <option value="6"> Item 6 </option>

      </select>

      <hr />

      HtmlSelect Size: <br />

      <select id="Select2" 
              runat="server">

         <option value="1" selected="selected"> 1 </option>
         <option value="2"> 2 </option>
         <option value="3"> 3 </option>
         <option value="4"> 4 </option>
         <option value="5"> 5 </option>
         <option value="6"> 6 </option>

      </select>

        

      <asp:CheckBox id="CheckBox1"
           Text="Enable Multiple Property"
           AutoPostBack="True"
           OnCheckedChanged="Check_Changed"
           Checked="True"
           runat="server"/>

      <br /><br />

      <button id="Button1"
              onserverclick="Button_Click"
              runat="server">

         Submit

      </button>

      <br /><br />

      <asp:Label id="Label1" runat="server"/>

   </div>
</form>
</body>
</html>

注解

Multiple使用 属性指定是否可以在 控件中HtmlSelect同时选择多个项。

默认情况下,控件 HtmlSelect 显示为下拉列表框。 如果通过将 属性设置为 Multipletrue) 允许 (多个选择,或者通过将属性设置为 Size 大于) 的值 1 来指定大于一行 (的高度,则控件将显示为列表框。

若要确定允许同时进行多个选择的控件中的 HtmlSelect 选定项,请循环访问 Items 集合并测试 ListItem.Selected 每个项的 属性。

注意

multiple仅当此属性设置为 true时,HtmlSelect属性才会在 控件中呈现。

适用于

另请参阅