다음을 통해 공유


HtmlSelect.Name 속성

정의

HtmlSelect 컨트롤에 연결된 고유 식별자 이름을 가져오거나 설정합니다.

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

속성 값

HtmlSelect 컨트롤에 연결된 고유 식별자 이름입니다.

예제

다음 코드 예제를 사용 Name 하는 방법에 설명 합니다 의 속성을 HtmlSelect 목록에서 특정 항목을 Items 선택 하는 컨트롤입니다.

<%@ Page Language="C#" 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" >
   <script runat="server" >
  
      void Page_Load(Object sender, EventArgs e)
      {

         // Bind a data source to the Repeater control. 
         Repeater1.DataSource = CreateRepeaterSource();
         Repeater1.DataBind();

      }

      void Item_Bound(Object sender, RepeaterItemEventArgs e)
      {

         // Each item in the Repeater control contains an HtmlSelect 
         // control. This method binds a data source to the HtmlSelect
         // control as each item in the Repeater control is being
         // bound to data.
 
         // The ItemDataBound event is raised when data is bound to an 
         // item in the Repeater control. Items can include the Header, 
         // Footer, and so on. Use the following logic only if the item 
         // being bound is an Item or AlternatingItem.
         if (e.Item.ItemType == ListItemType.Item || 
             e.Item.ItemType == ListItemType.AlternatingItem)
         {

            // Bind a data source to the HtmlSelect control.
            HtmlSelect selectControl = 
                (HtmlSelect)e.Item.FindControl("Select1");
            selectControl.DataSource = CreateHtmlSelectSource();
            selectControl.DataBind();

            // The runtime automatically generates a unique identifier 
            // for each control embedded in a list control, such as the 
            // Repeater. The Name property of the HtmlSelect control 
            // contains this unique identifier and is commonly used 
            // to identify a specific control.
           

            // Select the last item in the HtmlSelect control if the Name
            // property contains the value "Repeater1:_ctl3:Select1".
            if(selectControl.Name == "Repeater1:_ctl3:Select1") 
            {

               selectControl.SelectedIndex = selectControl.Items.Count - 1;

            }

         }

      }

      DataView CreateHtmlSelectSource()
      {

         // Create a DataTable that contains sample data for the 
         // HtmlSelect controls.
         DataTable dt = new DataTable();
         DataRow dr;
 
         dt.Columns.Add(new DataColumn("Text", typeof(String)));
         dt.Columns.Add(new DataColumn("Value", typeof(String)));
 
         // Populate the DataTable with sample values.
         for (int i = 0; i < 5; i++) 
         {
            dr = dt.NewRow();
 
            dr[0] = "Item " + i.ToString();
            dr[1] = i.ToString();
 
            dt.Rows.Add(dr);
         }
 
         // Create a DataView from the DataTable.
         DataView dv = new DataView(dt);
         return dv;

      }

      DataView CreateRepeaterSource()
      {

         // Create a DataTable that contains sample data for the 
         // Repeater control.
         DataTable dt = new DataTable();
         DataRow dr;
 
         dt.Columns.Add(new DataColumn("Category", typeof(String)));
 
         // Populate the DataTable with sample values.
         for (int i = 0; i < 5; i++) 
         {
            dr = dt.NewRow();
 
            dr[0] = "Category " + i.ToString();

            dt.Rows.Add(dr);
         }
 
         // Create a DataView from the DataTable.
         DataView dv = new DataView(dt);
         return dv;

      }
  
   </script>
  
<head runat="server">
    <title> HtmlSelect Name Example </title>
</head>
<body>

   <form id="form1" runat="server">
  
      <h3> HtmlSelect Name Example </h3>

      Notice that Category 3 has a different item selected by default. <br /> 
  
      <asp:Repeater id="Repeater1"
           OnItemDataBound="Item_Bound"
           runat="server">

         <ItemTemplate>

            <h4><%# DataBinder.Eval(Container.DataItem, "Category") %></h4>

            Select Item:

            <br />

            <select id="Select1"
                    datatextfield="Text"
                    datavaluefield="Value"
                    runat="server"/> 

            <br /><br />

            <hr />

         </ItemTemplate>

      </asp:Repeater>
  
   </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" >
   <script runat="server" >
  
      Sub Page_Load(sender as Object, e As EventArgs)

         ' Bind a data source to the Repeater control. 
         Repeater1.DataSource = CreateRepeaterSource()
         Repeater1.DataBind()

      End Sub

      Sub Item_Bound(sender As Object, e As RepeaterItemEventArgs)

         ' Each item in the Repeater control contains an HtmlSelect 
         ' control. This method binds a data source to the HtmlSelect
         ' control as each item in the Repeater control is being
         ' bound to data.
 
         ' The ItemDataBound event is raised when data is bound to an
         ' item in the Repeater control. Items can include the Header,
         ' Footer, and so on. Use the following logic only if the item 
         ' being bound is an Item or AlternatingItem.
         If (e.Item.ItemType = ListItemType.Item) Or _
            (e.Item.ItemType = ListItemType.AlternatingItem) Then

            ' Bind a data source to the HtmlSelect control.
            Dim selectControl As HtmlSelect = _
                CType(e.Item.FindControl("Select1"), HtmlSelect)
            selectControl.DataSource = CreateHtmlSelectSource()
            selectControl.DataBind()

            ' The runtime automatically generates a unique identifier 
            ' for each control embedded in a list control, such as the 
            ' Repeater. The Name property of the HtmlSelect control 
            ' contains this unique identifier and is commonly used 
            ' to identify a specific control.

            ' Select the last item in the HtmlSelect control if the Name 
            ' property contains the value "Repeater1:_ctl3:Select1".
            If selectControl.Name = "Repeater1:_ctl3:Select1" Then 

               selectControl.SelectedIndex = selectControl.Items.Count - 1

            End If

         End If

      End Sub

      Function CreateHtmlSelectSource() As DataView 

         ' Create a DataTable that contains sample data for the 
         ' HtmlSelect controls.
         Dim dt As DataTable = New DataTable()
         Dim dr As DataRow
 
         dt.Columns.Add(New DataColumn("Text", GetType(String)))
         dt.Columns.Add(New DataColumn("Value", GetType(String)))
 
         ' Populate the DataTable with sample values.
         Dim i As Integer
         
         For i = 0 To 4 
     
            dr = dt.NewRow()
 
            dr(0) = "Item " & i.ToString()
            dr(1) = i.ToString()
 
            dt.Rows.Add(dr)
         
         Next i
 
         ' Create a DataView from the DataTable.
         Dim dv As DataView = New DataView(dt)
         Return dv

      End Function

      Function CreateRepeaterSource() As DataView 

         ' Create a DataTable that contains sample data for the 
         ' Repeater control.
         Dim dt As DataTable = New DataTable()
         Dim dr As DataRow
 
         dt.Columns.Add(new DataColumn("Category", GetType(String)))
 
         ' Populate the DataTable with sample values.
         Dim i As Integer

         For i = 0 To 4  

            dr = dt.NewRow()
 
            dr(0) = "Category " & i.ToString()

            dt.Rows.Add(dr)

         Next i
 
         ' Create a DataView from the DataTable.
         Dim dv As DataView = new DataView(dt)
         return dv

      End Function
  
   </script>
  
<head runat="server">
    <title> HtmlSelect Name Example </title>
</head>
<body>

   <form id="form1" runat="server">
  
      <h3> HtmlSelect Name Example </h3>

      Notice that Category 3 has a different item selected by default. <br /> 
  
      <asp:Repeater id="Repeater1"
           OnItemDataBound="Item_Bound"
           runat="server">

         <ItemTemplate>

            <h4><%# DataBinder.Eval(Container.DataItem, "Category") %></h4>

            Select Item:

            <br />

            <select id="Select1"
                    datatextfield="Text"
                    datavaluefield="Value"
                    runat="server"/> 

            <br /><br />

            <hr />

         </ItemTemplate>

      </asp:Repeater>
  
   </form>

</body>
</html>

설명

사용 된 Name 속성에 대 한 고유 식별자 이름을 확인 하는 컨트롤입니다 HtmlSelect . 이 구현에서는 합니다 get 의 값을 반환 하는 접근자를 Control.UniqueID 속성. 그러나는 set 접근자가이 속성에 값을 할당 하지 않습니다.

참고

속성이 set 컨트롤이 제대로 작동하려면 속성 Name 과 동일한 값을 가져야 하므로 접근자는 이 속성에 HtmlSelect 값을 Control.UniqueID 할당하지 않습니다.

적용 대상

추가 정보