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 Mike Lewis 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 Dynamic DropDownList

Status
Not open for further replies.

ralphiooo

Programmer
Oct 23, 2005
64
0
0
GB
Hi i have a FormView control with a DropDownList in like so:

<asp:DropDownList ID="lstCategoryID" DataTextField="Text" DataValueField="Value" runat="server">
</asp:DropDownList>

And in the Page_Load event handler i have the following:

Dim lstCategoryID As DropDownList = CType(FormView1.FindControl("lstCategoryID"), DropDownList)

lstCategoryID.DataSource = Utilities.GetCategoriesList(_sectionID, 0, "")
lstCategoryID.DataBind()

Which populates the DropDownList fine but i need to select the appropriate value based on the CategoryID for the item i am editing.

I tried putting SelectedValue='<%# Eval("CategoryID") %>' on the dropdownlist but it returned the following error.

"'lstCategoryID' has a SelectedValue which is invalid because it does not exist in the list of items"

The trouble is the value for CategoryID is the item i am editing is in the DropDownList. I have had no problem in the past when the DropDownList was statically populated. Therefore i think the problem might be that i am binding the data in the code behind file but i don't know how to solve it.

Appreciate your help. Thanks
 
Is your drop down inside your formview or outside
 
Inside the FormView that's why i had to put:

Dim lstCategoryID As DropDownList = CType(FormView1.FindControl("lstCategoryID"), DropDownList)
 
Once you have a reference to the DropDownList, try using:
Code:
myDropDownList.Items.FindByValue("myValue").Selected = True


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top