Export (0) Print
Expand All

HtmlSelect.Name Property

Gets or sets the unique identifier name associated with the HtmlSelect control.

Namespace:  System.Web.UI.HtmlControls
Assembly:  System.Web (in System.Web.dll)

public string Name { get; set; }
<asp:HtmlSelect Name="String" />

Property Value

Type: System.String
The unique identifier name associated with the HtmlSelect control.

Use the Name property to determine the unique identifier name for an HtmlSelect control. In this implementation, the get accessor returns the value of the Control.UniqueID property. However, the set accessor does not assign a value to this property.

NoteNote:

The set accessor does not assign a value to this property because the Name property must have the same value as the Control.UniqueID property for the HtmlSelect control to work properly.

The following code example demonstrates how to use the Name property of the HtmlSelect control to select a specific item in the Items list.

<%@ 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>

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft