ListControl.SelectedValue 속성

정의

목록 컨트롤에서 선택한 항목의 값을 가져오거나, 지정된 값이 포함된 목록 컨트롤에서 항목을 선택합니다.

public:
 virtual property System::String ^ SelectedValue { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.Bindable(true)]
[System.ComponentModel.Browsable(false)]
public virtual string SelectedValue { get; set; }
[System.ComponentModel.Browsable(false)]
[System.ComponentModel.Bindable(true, System.ComponentModel.BindingDirection.TwoWay)]
[System.Web.UI.Themeable(false)]
public virtual string SelectedValue { get; set; }
[<System.ComponentModel.Bindable(true)>]
[<System.ComponentModel.Browsable(false)>]
member this.SelectedValue : string with get, set
[<System.ComponentModel.Browsable(false)>]
[<System.ComponentModel.Bindable(true, System.ComponentModel.BindingDirection.TwoWay)>]
[<System.Web.UI.Themeable(false)>]
member this.SelectedValue : string with get, set
Public Overridable Property SelectedValue As String

속성 값

String

목록 컨트롤에서 선택한 항목의 값입니다. 기본값은 빈 문자열("")입니다.

특성

예외

선택한 값이 사용 가능한 값의 목록에 있지 않고 보기 상태 또는 다른 상태가 로드되지 않았습니다. 그리고 포스트백은 수행되었습니다.

예제

다음 예제에서는 컨트롤에서 SelectedValue 항목을 선택 하는 속성을 사용 하는 방법을 보여 줍니다 ListBox . 이 속성을 사용하여 선택한 항목의 값을 검색할 수도 있습니다.

중요

이 예제에는 사용자 입력을 허용하는 텍스트 상자가 있으므로 보안상 위험할 수 있습니다. 기본적으로 ASP.NET 웹 페이지는 사용자 입력 내용에 스크립트 또는 HTML 요소가 포함되어 있지 않은지 확인합니다. 자세한 내용은 Script Exploits Overview를 참조하세요.


<%@ 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> ListControl SelectedValue Example </title>
<script runat="server">

      void Button_Click(Object sender, EventArgs e)
      {

         // Perform this operation in a try-catch block in case the item is not found.
         try
         {
            List.SelectedValue = ItemTextBox.Text;
            MessageLabel.Text = "You selected " + List.SelectedValue + ".";
         }
         catch (Exception ex)
         {
            List.SelectedValue = null;
            MessageLabel.Text = "Item not found in ListBox control.";
         }
             
      }

   </script>

</head>

<body>

   <form id="form1" runat="server">

      <h3> ListControl SelectedValue Example </h3>
 
      <asp:ListBox ID="List"
           runat="server">

         <asp:ListItem>Item 1</asp:ListItem>
         <asp:ListItem>Item 2</asp:ListItem>
         <asp:ListItem>Item 3</asp:ListItem>
         <asp:ListItem>Item 4</asp:ListItem>

      </asp:ListBox>

      <hr />

      Enter the value of the item to select: <br />
      <asp:TextBox ID="ItemTextBox"
           MaxLength="6"
           Text="Item 1"
           runat="server"/>

        

      <asp:Button ID="SelectButton"
           Text="Select Item"
           OnClick="Button_Click"
           runat="server"/>

      <br /><br />

      <asp:Label ID="MessageLabel"
           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> ListControl SelectedValue Example </title>
<script runat="server">

      Sub Button_Click(sender As Object, e As EventArgs)

         ' Perform this operation in a try-catch block in case the item is not found.
         Try
   
            List.SelectedValue = ItemTextBox.Text
            MessageLabel.Text = "You selected " & List.SelectedValue + "."
        
         Catch ex As Exception
     
            List.SelectedValue = Nothing         
            MessageLabel.Text = "Item not found in ListBox control."
     
         End Try
             
      End Sub

   </script>

</head>

<body>

   <form id="form1" runat="server">

      <h3> ListControl SelectedValue Example </h3>
 
      <asp:ListBox ID="List"
           runat="server">

         <asp:ListItem>Item 1</asp:ListItem>
         <asp:ListItem>Item 2</asp:ListItem>
         <asp:ListItem>Item 3</asp:ListItem>
         <asp:ListItem>Item 4</asp:ListItem>

      </asp:ListBox>

      <hr />

      Enter the value of the item to select: <br />
      <asp:TextBox ID="ItemTextBox"
           MaxLength="6"
           Text="Item 1"
           runat="server"/>

        

      <asp:Button ID="SelectButton"
           Text="Select Item"
           OnClick="Button_Click"
           runat="server"/>

      <br /><br />

      <asp:Label ID="MessageLabel"
           runat="server"/>     

   </form>

</body>
</html>

설명

이 속성은 선택한 ListItem속성 Value 의 속성을 반환합니다. 이 SelectedValue 속성은 일반적으로 목록 컨트롤에서 선택한 항목의 값을 확인하는 데 사용됩니다. 여러 항목을 선택하면 인덱스가 가장 낮은 선택한 항목의 값이 반환됩니다. 항목을 선택하지 않으면 빈 문자열("")이 반환됩니다.

속성은 SelectedValue 항목의 값으로 설정하여 목록 컨트롤에서 항목을 선택하는 데 사용할 수도 있습니다.

이 속성은 테마 또는 스타일시트 테마에 의해 설정될 수 없습니다. 자세한 내용은 ThemeableAttribute 하 고 ASP.NET 테마 및 스킨합니다.

선택한 값이 사용 가능한 값 목록에 없으며 포스트백이 수행되면 예외가 ArgumentOutOfRangeException throw됩니다. 다음 예제에서는 포스트백이 발생하기 전에 잘못된 값을 catch하는 방법을 보여 줍니다.

Me.DropDownList1.Items.Add(New ListItem( Text="Hello", Value="1" ))
If DropDownList1.Items.FindByValue("2") IsNot Nothing Then
    Response.Write("Found")
End If
this.DropDownList1.Items.Add(new ListItem{ Text="Hello", Value="1" });
if(DropDownList1.Items.FindByValue("2") != null) {
    Response.Write("Found");
}

적용 대상

추가 정보