How do we get combo box's selected value in asp.net as we use to do in classic asp??

Dani AI

Generated

As hinted, the value you read from a DropDownList comes from the item Value (the DataValueField you set when binding). ended up binding a Hashtable and used lowercase field names, which is the common gotcha here.

When you bind a Hashtable it enumerates DictionaryEntry objects that expose properties named "Key" and "Value" (capitalized). If DataValueField/DataTextField do not match those exact property names the control will not use the intended value. Two practical fixes:

  • Set the fields to the correct names when binding a Hashtable: use "Key" for the value and "Value" for the text.
  • Or populate the list with ListItem objects so you explicitly control Text and Value.

Example (explicit add, VB-style shown conceptually):

' add items explicitly so Text and Value are guaranteed
cboCompany.Items.Clear()
cboCompany.Items.Add(New ListItem(companyName, companyCode))

Quick troubleshooting checklist:

  • After DataBind, inspect the rendered HTML (view-source) — each <option> should show the value attribute you expect.
  • Inspect cboCompany.Items in the debugger (check .Text vs .Value).
  • Ensure you only bind on first load (If Not Page.IsPostBack Then ... DataBind()) so ViewState preserves the selection on postback.
  • If you need stable ordering, use a DataTable or List(Of T) rather than Hashtable (Hashtable does not guarantee order).

Further reading: ListControl.SelectedValue behavior and the DictionaryEntry type on Microsoft Docs: ListControl.SelectedValue and DictionaryEntry.

You can SelectedValue of Dropdownlist.

Dim str as String
str= DropDownlist1.SelectedValue

You can SelectedValue of Dropdownlist.

Dim str as String
str= DropDownlist1.SelectedValue

Hi,

Thanks for the reply but that gives the text not the key value,so i created a hash table and got the value pair like this,

If Not Page.IsPostBack Then
            get_data.execute_Sql("SELECT CCODE,COMPANY_NAME FROM COMPANY")

            While get_data.dr.Read
                arrData.Add(get_data.dr.Item(0), get_data.dr.Item(1))
            End While

            With cboCompany
                .DataSource = arrData
                .DataValueField = "key"
                .DataTextField = "value"
                .DataBind()
            End With
        End If

        MsgBox(cboCompany.SelectedValue)

Thanks for the reply...

Sabarish

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.