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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binding to DropDownList problems

Status
Not open for further replies.

raphael232

Programmer
Jun 9, 2006
51
0
0
GB
Hi, i'm having problems binding to the DropDownList control. Here's my code:

Code:
<asp:DropDownList ID="lstCategoryID" SelectedValue='<%# Eval("CategoryID") %>' DataTextField="Text" DataValueField="Value" runat="server">
</asp:DropDownList>

and here's my code behind (Page_Load event handler):

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

lstCategoryID.DataSource = Utilities.GetCategoriesList()
lstCategoryID.DataBind()

When i run the application i get the error:

'lstCategoryID' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

This seems a strange error since there is a value in the DropDownList which is the same as the value i am trying to bind to. I think it might be something to do with me loading in the contents of the DropDownList with the Page_Load event handler (since i have had no problems with dropdownlists with static options). I have also tried putting in the Page_Init event handler but this did not work either.

Appreciate if someone could help. Thanks
 
>>SelectedValue='<%# Eval("CategoryID") %

will try to read the value from a field called "CategoryId" in the dataset returned by Utilities.GetCategoriesList() method.

why are u using eval there???


Known is handfull, Unknown is worldfull
 
As vbkris said, you dont need to use Eval there, set it when you bind the list in the code behind.

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

lstCategoryID.DataSource = Utilities.GetCategoriesList()
lstCategoryID.SelectedValue = "CategoryID"
lstCategoryID.DataTextField = "Text"
lstCategoryID.DataBind()

Web Design Wetherby
Personal Web Design Service
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top