Export (0) Print
Expand All

HtmlSelect.DataValueField Property

Gets or sets the field from the data source to bind to the ListItem.Value property of each item in the HtmlSelect control.

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

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

Property Value

Type: System.String
The field from the data source to bind to the ListItem.Value property of each item in the HtmlSelect control. The default value is an empty string (""), which indicates that the property has not been set.

Use the DataValueField property to specify which field from the data source to bind to the ListItem.Value property of each item in the control. This property is commonly used to provide a value for the ListItem.Value property that differs from the value of the ListItem.Text property.

The HtmlSelect class provides two properties for specifying the data source to bind to. The DataSource property allows you to bind an HtmlSelect control to any collection that implements the IEnumerable or IListSource interfaces (such as System.Data.DataView, System.Collections.ArrayList, or System.Collections.Generic.List<T>). When you use the DataSource property to specify the data source, you must explicitly call the DataBind method to bind the control and its DataValueField property to the data source.

The DataSourceID property allows you to bind an HtmlSelect control to a data source control that represents a data source. When you use the DataSourceID property to specify the data source, the HtmlSelect control and its DataValueField property automatically bind to the data source control. Therefore, you do not need to explicitly call the DataBind method.

The following code example demonstrates how to use the DataSource and DataValueField properties to specify the field from the data source to bind to the ListItem.Value property of each item in the HtmlSelect control.

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

           // Open a connection to the database and run the query.
           // Note that the connection string may vary depending on your
           // database server settings. 
           string ConnectString = "server=localhost;database=pubs;integrated security=SSPI";
           string QueryString = "select * from authors";

           SqlConnection myConnection = new SqlConnection(ConnectString);
           SqlDataAdapter myCommand = new SqlDataAdapter(QueryString, myConnection);

           // Create a dataset to store the query results.
           DataSet ds = new DataSet();
           myCommand.Fill(ds, "Authors");

           // Bind the HtmlSelect control to the data source.
           Select1.DataSource = ds;
           Select1.DataTextField = "au_fname";
           Select1.DataValueField = "au_fname";
           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 /> &nbsp;&nbsp; - " + 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>

The following code example demonstrates how to use the DataSourceID and DataValueField properties to specify the field from the data source to bind to the ListItem.Value property of each item in the HtmlSelect control.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server" language="C#">

  void SubmitButton_Click (object sender, System.EventArgs e)
  {
    // Iterate through the list items in the 
    // HtmlSelect control to find the selected item.
    for (int i = 0; i <= Select1.Items.Count - 1; i++)
    {
      if (Select1.Items[i].Selected)
        // Display the Value property of the selected item.
        // This property is populated by the DataValueField property.
        Label1.Text = "The Product ID is " + Select1.Items[i].Value;
    }
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>HtmlSelect.DataValueField</title>
</head>
<body>
  <form id="Form1" runat="server">  

    <h3> HtmlSelect.DataValueField Example 2</h3>

    <p>Select an item from the list:</p>

    <select id="Select1"
      name="Select1"             
      datasourceid="SqlDataSource1"
      datatextfield="ProductName"
      datavaluefield="ProductID"
      runat="server">
    </select>

    <asp:sqldatasource id="SqlDataSource1"          
          connectionstring="workstation id=localhost;integrated security=SSPI;initial catalog=Northwind"
      selectcommand="SELECT * FROM [Products] Where ProductID <= 5"
      runat="server">
    </asp:sqldatasource>

    <asp:button id="SubmitButton"
      text="Submit"
      onclick="SubmitButton_Click"
      runat="Server">
    </asp:button>

    <br /><br />

    <asp:label id="Label1"
      runat="Server">
    </asp:label>

   </form>

</body>

</html>

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft