Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting value from a already populated dropdownlist

Status
Not open for further replies.
Apr 11, 2002
193
IN
Hi,

I have a dropdownlist which i am populating at the page_load with countries. The same page is used for entering new records as well as updating existing records. So when i search for a particular id it takes me to this form and existing record are loaded in the controls of the page. At page_load countries are already loaded in a dropdownlist. What i want is to display the selected id's country rather then using ddl.selectedItem.Text = dr["Country"].toString(). This will add one more record rather than selecting an existing record. Can this be possible without a for loop.

Thanks,
Manish
 
Code:
ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText(dr["Country"].toString()))

how's this?

"...we both know I'm training to become a cagefigher...see what happens if you try 'n hit me..."
 
Hi,
The code from checkai (heh, cute name) should work, but you may want to do a ddl.ClearSelection() first b4 setting it coz sometimes, it gives you error like [dropdownlist cannot have multiple item selected]. STupid problem, but does happen :) cheers.

regards,
- Joseph

~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
 
I had a similar problem where I was getting an error message like:
Code:
"Description:  System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: at System.Web.UI.WebControls.ListControl.set_SelectedValue(String value) at SatelliteTransmission.CompanyDetail.Page_Load(Object sender, EventArgs e)

Number:   5

Source:   System.Web

I used some of Checkai's code, but I was calling my dataset a different way:

Code:
Dim cmd As New SQLStoredProcedure(AppSettings("cnn"))
Dim Company As Integer = CInt(Request.QueryString("id"))
Dim Tbl As New DataTable
Dim Row As DataRow
Dim Item As ListItem
Dim obj As New LoadSelections.OjbectToLoad

obj.Load(cboST, cmd.GetData("sp_Lookup_s", 9, False)) 'load states

If Not Company = 0 Then 'load company information
lnkContacts.NavigateUrl = "contacts.aspx?coid=" & Company
Row = cmd.GetData("sp_Address_s", Company, False).Rows(0)

If Not IsDBNull(Row.Item(5)) Then
   cboST.SelectedIndex = cboST.Items.IndexOf(cboST.Items.FindByText(Row.Item(5)))
Else
   cboST.SelectedValue = ""
End If


Thanks Checkai!

"Politeness costs nothing, and gains everything." - the last fortune cookie I ate.

the dude abides...
 
a better options would be to use ispostback option.

bind the control only if the page isnt posted back to itself. The view state will take care of the rest(selecting the corrrect item) etc.

hope that helped...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top