HtmlSelect.DataValueField Property
Gets or sets the field from the DataSource to bind to the ListItem.Value property of each item in the HtmlSelect control.
[Visual Basic] Public Overridable Property DataValueField As String [C#] public virtual string DataValueField {get; set;} [C++] public: __property virtual String* get_DataValueField(); public: __property virtual void set_DataValueField(String*); [JScript] public function get DataValueField() : String; public function set DataValueField(String);
Property Value
The field from the DataSource to bind to the ListItem.Value property of each item in the HtmlSelect control.
Remarks
Use the DataValueField property to specify which field from the DataSource 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.
Example
[Visual Basic, C#, JScript] The following example demonstrates how to use the DataValueField property to specify the field from the DataSource to bind to the ListItem.Value property of each item in the HtmlSelect control.
[Visual Basic] <%@ Page Language="VB" AutoEventWireup="True" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <html> <head> <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 ' Open a connection to the database and run the query. ' Note that the connection string may vary depending on your ' database server settings. Dim ConnectString As String = "server=localhost;database=pubs;integrated security=SSPI" Dim QueryString As String = "select * from authors" Dim myConnection As SqlConnection = New SqlConnection(ConnectString) Dim myCommand As SqlDataAdapter = New SqlDataAdapter(QueryString, myConnection) ' Create a dataset to store the query results. Dim ds As DataSet = 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() End If End Sub Sub Button_Click (sender As Object, e As EventArgs) Dim i As Integer 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 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> [C#] <%@ Page Language="C#" AutoEventWireup="True" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <html> <head> <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> - " + Select1.Items[i].Text; } } </script> </head> <body> <form 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> [JScript] <%@ Page Language="JScript" AutoEventWireup="True" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <html> <head> <script runat="server"> function Page_Load (sender : Object, e : EventArgs) : void { // 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. var ConnectString : String = "server=localhost;database=pubs;integrated security=SSPI"; var QueryString : String = "select * from authors"; var myConnection : SqlConnection = new SqlConnection(ConnectString); var myCommand : SqlDataAdapter = new SqlDataAdapter(QueryString, myConnection); // Create a dataset to store the query results. var ds : DataSet = 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(); } } function Button_Click (sender : Object, e : EventArgs) : void { // Display the selected items. Label1.Text = "You selected:"; for (var i: int=0; i<=Select1.Items.Count - 1; i++) { if (Select1.Items[i].Selected) Label1.Text += "<br> - " + Select1.Items[i].Text; } } </script> </head> <body> <form 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>
[C++] No example is available for C++. To view a Visual Basic, C#, or JScript example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
See Also
HtmlSelect Class | HtmlSelect Members | System.Web.UI.HtmlControls Namespace | DataSource | ListItem.Value | DataTextField