Suppose that we are using DropDownList to display a list of countries and we want the control to return the country id of currently selected country. We can do so by using the DataValueField property of a list item.
If we are using a datatable for binding DropDownList control then you can set datavaluefield as:
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %> <%@ Import Namespace="System.Data" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> Sub Page_Load() If Not IsPostBack Then Dim dt As New DataTable dt.Columns.Add("id") dt.Columns.Add("Country") Dim rw As DataRow rw = dt.NewRow() rw(0) = "1" rw(1) = "USA" dt.Rows.Add(rw) rw = dt.NewRow() rw(0) = "2" rw(1) = "India" dt.Rows.Add(rw) rw = dt.NewRow() rw(0) = "3" rw(1) = "Japan" dt.Rows.Add(rw) DropDownList1.DataSource = dt DropDownList1.DataValueField = "id" DropDownList1.DataTextField = "Country" DropDownList1.DataBind() End If End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>Find Selected item in DropDownList control</title> </head> <body style="height: 410px; width: 768px"> <form runat="server"> <asp:DropDownList ID="DropDownList1" runat="server" > </asp:DropDownList> </form> </body> </html>
If we bind DropDownList control as the time of declaration then you can set datavaluefield as:
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %> <!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 id="Head1" runat="server"> <title>Find Selected item in DropDownList control</title> </head> <body style="height: 410px; width: 768px"> <form runat="server"> <asp:DropDownList ID="DropDownList1" runat="server" > <asp:ListItem Text="USA" Value="1" /> <asp:ListItem Text="India" Value="2" /> <asp:ListItem Text="Japan" Value="3" /> </asp:DropDownList> </form> </body> </html>