Click here to Skip to main content
16,010,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!!!
I have a dropdownlist having a sqldatasource with DataValueField and DataTextField set.

Now what i need to do is when a record is selected for editing , dropdown list now should show the value that was there for that record.

for example if the record in the database was "Australia" then the dropdownlist should show "Australia" as selected index.

please tell me how to do this!!!
Posted
Updated 19-Jul-12 20:05pm
v2

There's a selectedindex and a selectedvalue property. You can set those.
 
Share this answer
 
Comments
ujjwal uniyal 20-Jul-12 2:16am    
I used selected index property but everytime it's showing me that the selected index is -1

i tried this
Dim lst As ListItem
With dt.Rows(0)

lst = New ListItem(Dal.ExScalar("Select state_name from master_state where state_id=" + .Item("state_permanent_address").ToString() + ""), .Item("state_permanent_address").ToString())
ddlState_Per.SelectedIndex = ddlState_Per.Items.IndexOf(lst)
End With
Tejas Vaishnav 20-Jul-12 2:20am    
you can try to set property at design time,
like this, try to add this to your dropdwon list design
SelectedValue = '<%# Eval("YOUR DATAVALUE FILED")%>';
Tejas Vaishnav 20-Jul-12 2:17am    
perfect answer
ujjwal uniyal 20-Jul-12 2:31am    
i am using same panel for saving data and then the same panel for updating data. Also there is a sqldatasource which i have used to bind list so i can't use Eval i think..
Christian Graus 20-Jul-12 2:32am    
If you're using a SqlDataSource which you drop in from the toolbox, it may inhibit what you can do. that's one of the many reasons that no real programmer uses them.
 
Share this answer
 
Comments
ujjwal uniyal 20-Jul-12 2:17am    
I used selected index property but everytime it's showing me that the selected index is -1

i tried this
Dim lst As ListItem
With dt.Rows(0)

lst = New ListItem(Dal.ExScalar("Select state_name from master_state where state_id=" + .Item("state_permanent_address").ToString() + ""), .Item("state_permanent_address").ToString())
ddlState_Per.SelectedIndex = ddlState_Per.Items.IndexOf(lst)
End With
Christian Graus 20-Jul-12 2:24am    
That is bizarre code. Why do you need to create a new list item ? Why do you think the new list item which is not in your list, has an index ?
ujjwal uniyal 20-Jul-12 3:04am    
Thanks Christian. I solved the problem. I hadn't bind the list before searching for the selected index when editing the records that why the problem was coming. your comment above guided me in the right direction.
Hi I thought you are developing a Windows based app
what ever its so easy to start
don't get confused I thought you might knew this already

dropdownlist click property you can use
after that fetch the selected value using selecedvalue/selectedItem
If you want you can fetch the selctedIndex value

then prepare your database statement and include where condition in it fetch the exact details.

then you might know what to do with it...
 
Share this answer
 
The Process that i was using was right the only problem that i was facing was because that i hadn't bind the list before searching for the selected index. That's why it was showing selected index as -1 every time.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900