El campo de valor de datos de la lista desplegable no funciona

i am getting the datas from database and storing in a collectionobject. The collectionobj contains the Datas 'Name' and 'ID'.

Mi código:

<asp:dropdownlist id="dropdown1" runat="server" DataTextField="Name" DataValueField="ID"/>


collectionobj=objbs.GetNAmes()

for intloop as integer in Collectionobj.Count.Rows-1 

dropdown.Items.Add( Collectionobj.Items(intloop).Name)
dropdown.DataValueField=Collectionobj.Items(intloop).ID

Next
dropdown.DataBind()

Im getting the Value binded in the dropdown as "Names" . when i select the dropdown i need to pass the respective ID of that name.

But when I assign the

Dim strid as String=dropdown1.DataValueField

La dropdown1.DataValueField is holding the id of last name binded in the dropdown.it is not taking the id which im selecting n the dropdown..

Any suggestion..........

preguntado el 08 de enero de 11 a las 16:01

Your code is a mess, and trying to format it just makes it all that much more evident. VB.NET will automatically capitalize most of those identifiers, so that tells me you're not copying and pasting this from the IDE, which is your first mistake. -

1 Respuestas

You should use SelectedValue property not DataValueField property to get the curent selected value.

Dim strid as String=dropdown1.SelectedValue

Since you mentioned getNAmes returns a colection, you can replace the code to populate the dropdown with this:

collectionobj=objbs.GetNAmes()
dropdown.DataSource=Collectionobj //The datanamefield and value field are specified in aspx file.
dropdown.DataBind()


To get the Selected Text use: dropdown.SelectedItem.Text
To get the Selected Value use: dropdown.SelectedItem.Value or dropdown.SelectedValue

Respondido el 08 de enero de 11 a las 20:01

but the dropdown1.SelectedValue is even giving me oly the Name. and not the ID . for eg. it is giving me oly 'XXX' and not the repective '1' id. - Innova

What does objbs.GetNAmes() return? - Chandu

collectionobject with all the property values set to it - Innova

tnks for yr rwply. even i tried this the value in strid is simply the text "id" when i use this code . Dim strid as String=dropdown1.DataValueField - Innova

As I mentioned earlier you should use dropDown.SelectedValue not DataValueField. - Chandu

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.