dropdownlist's Datavaluefield is not working
i am getting the datas from database and storing in a collectionobject. The collectionobj contains the Datas 'Name' and 'ID'.
My code:
<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
The 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..........
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
精彩评论