다음을 통해 공유


HtmlSelect.DataMember 속성

정의

여러 데이터 집합이 있는 HtmlSelect 속성에서 DataSource 컨트롤에 바인딩할 데이터 집합을 가져오거나 설정합니다.

public:
 virtual property System::String ^ DataMember { System::String ^ get(); void set(System::String ^ value); };
public virtual string DataMember { get; set; }
member this.DataMember : string with get, set
Public Overridable Property DataMember As String

속성 값

String

여러 데이터 집합이 있는 HtmlSelect에서 DataSource 컨트롤에 바인딩할 데이터 집합입니다. 기본값은 빈 문자열("")로, 이 속성이 설정되지 않았음을 나타냅니다.

예외

DataMember 컨트롤을 데이터 바인딩하는 동안 HtmlSelect 속성이 설정된 경우

예제

다음 코드 예제에서는 여러 데이터 집합을 사용 하 여 속성에서 DataSource 컨트롤에 바인딩할 데이터 집합을 지정 하는 HtmlSelect 속성을 사용 DataMember 하는 방법을 보여 줍니다.

<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<!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>
    <title> HtmlSelect Example </title>
<script runat="server">

      void Page_Load (Object sender, EventArgs e)
      {

         // Bind the HtmlSelect control to a data source when the page is initially loaded.
         if (!IsPostBack)
         {
      
            // Create a data set to store the tables.
            DataSet ds = new DataSet("TitleDataSet");
            DataRow dr; 

            // Create the authors table with a single column.
            DataTable authors = new DataTable("Authors");            
            authors.Columns.Add(new DataColumn("LName", typeof(string)));                    
 
            // Add the authors table to the data set.
            ds.Tables.Add(authors);

            // Create sample data in the authors table.
            for (int i = 0; i < 9; i++) 
            {
               // Create a new row for the authors table.
               dr = authors.NewRow();

               // Add data to the LName column (column 0 in the row).
               dr[0] = "Author " + i.ToString();

               // Insert the row into the authors table.   
               authors.Rows.Add(dr);
            }
  
            // Create the titles table with a single column.
            DataTable titles = new DataTable("Titles");            
            titles.Columns.Add(new DataColumn("BookTitle", typeof(string)));                    
 
            // Add the titles table to the data set.
            ds.Tables.Add(titles);

            // Create sample data in the titles table.
            for (int i = 0; i < 9; i++) 
            {
               // Create a new row for the titles table.
               dr = titles.NewRow();

               // Add data to the BookTitle column (column 0 in the row).
               dr[0] = "Title " + i.ToString();

              // Insert the row into the titles table.   
              titles.Rows.Add(dr);
            }

            // Bind the HtmlSelect control to the data source.
            Select1.DataSource = ds;
            Select1.DataMember = "Titles";
            Select1.DataTextField = "BookTitle";
            Select1.DataBind();

         }

      }

      void Button_Click (Object sender, EventArgs e)
      {
       
         // Display the selected items. 
         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;
         }

      }

   </script>

</head>

<body>

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

      <h3> HtmlSelect Example </h3>

      Select items from the list. <br />
      Use the Control or Shift key to select multiple items. <br /><br />

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

      <br /><br />

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

         Submit

      </button>

      <br /><br />

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

   </form>

</body>

</html>
<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>

<!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>
    <title> HtmlSelect Example </title>
<script runat="server">

      Sub Page_Load (sender As Object, e As EventArgs)

         ' Bind the HtmlSelect control to a data source when the page is initially loaded.
         If Not IsPostBack Then
 
            Dim i As Integer
      
            ' Create a data set to store the tables.
            Dim ds As DataSet = New DataSet("TitleDataSet")
            Dim dr As DataRow 

            ' Create the authors table with a single column.
            Dim authors As DataTable = New DataTable("Authors")            
            authors.Columns.Add(New DataColumn("LName", GetType(String)))                    
 
            ' Add the authors table to the data set.
            ds.Tables.Add(authors)

            ' Create sample data in the authors table.
            For i = 0 To 9 
           
               ' Create a new row for the authors table.
               dr = authors.NewRow()

               ' Add data to the LName column (column 0 in the row).
               dr(0) = "Author " & i.ToString()

               ' Insert the row into the authors table.   
               authors.Rows.Add(dr)

            Next i
  
            ' Create the titles table with a single column.
            Dim titles As DataTable = New DataTable("Titles")            
            titles.Columns.Add(New DataColumn("BookTitle", GetType(String)))                    
 
            ' Add the titles table to the data set.
            ds.Tables.Add(titles)

            ' Create sample data in the titles table.
            For i = 0 To 9
            
               ' Create a new row for the titles table.
               dr = titles.NewRow()

               ' Add data to the BookTitle column (column 0 in the row).
               dr(0) = "Title " & i.ToString()

               ' Insert the row into the titles table.   
               titles.Rows.Add(dr)

            Next i

            ' Bind the HtmlSelect control to the data source.
            Select1.DataSource = ds
            Select1.DataMember = "Titles"
            Select1.DataTextField = "BookTitle"
            Select1.DataBind()

         End If

      End Sub

      Sub Button_Click (sender As Object, e As EventArgs)

         Dim i As Integer
       
         ' Display the selected items. 
         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 i

      End Sub

   </script>

</head>

<body>

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

      <h3> HtmlSelect Example </h3>

      Select items from the list. <br />
      Use the Control or Shift key to select multiple items. <br /><br />

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

      <br /><br />

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

         Submit

      </button>

      <br /><br />

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

   </form>

</body>

</html>

설명

속성에 DataSource 여러 데이터 집합이 포함된 경우 속성을 사용하여 DataMember 컨트롤에 바인딩할 데이터 집합을 지정합니다. 예를 들어 여러 테이블이 있는 System.Data.DataSet 개체가 있는 경우 이 속성을 사용하여 컨트롤에 바인딩할 테이블을 나타냅니다.

컨트롤에 바인딩할 데이터 원본을 지정한 후 각각 및 속성을 설정 DataTextField 하여 컨트롤의 각 항목에 바인딩할 ListItem.Text 데이터 원본의 필드와 ListItem.Value DataValueField 속성을 지정할 수 있습니다.

적용 대상

추가 정보