This is a migrated thread and some comments may be shown as answers.

FindItemByValue not selecting correctly

3 Answers 244 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Shane Clay
Top achievements
Rank 1
Shane Clay asked on 12 Oct 2010, 05:17 PM
Hi Guys,

I think this is a simple one - I'm missing something basic.

I'm running the following;

' The item exists, so select it
inDDL.SelectedIndex = inDDL.FindItemIndexByValue(selectValue)

However Im not getting the right result. When I look at the control after this code has executed the combo box shows the first item in the list as opposed to the one I have just selected. If I exapnd the drop down list so I can see all the items, the one I have "selected" in my code is highlighted, but not the one shown when the box is not expanded.

How can I make the selected item the one that is shown in the collapsed combo box?

Shane

3 Answers, 1 is accepted

Sort by
0
Shane Clay
Top achievements
Rank 1
answered on 13 Oct 2010, 04:56 AM

This is not happening now. I have changed the control from binding to an SQL data source to being done this way;

Dim sqlString As String = "SELECT [wtID], [wtName] FROM [lstWorkTeam];"
Dim sqlCMD As SqlCommand = New SqlCommand(sqlString, dbConn)
Dim sqlResult As SqlDataReader = sqlCMD.ExecuteReader
inDDL.DataSource = sqlResult
inDDL.DataValueField = "wtID"
inDDL.DataTextField = "wtName"
inDDL.DataBind()
sqlResult.Close()

With control;

<telerik:RadComboBox ID="rcbWorkTeam" Runat="server">
</telerik:RadComboBox>

The control is now behaving as expected.

Shane
0
Cori
Top achievements
Rank 2
answered on 13 Oct 2010, 01:37 PM
Hello Shane,

If you were binding the RadComboBox to an SqlDataSource control, you needed to call DataBind on the RadComboBox, before setting the SelectedIndex. Like so:

inDDL.DataBind();
inDDL.SelectedIndex = inDDL.FindItemIndexByValue(selectValue);

The reason for this is that the control has not been databound at that point, which I'm assuming your calling this in the Page_Load event.

I hope that helps.
0
Shane Clay
Top achievements
Rank 1
answered on 13 Oct 2010, 03:14 PM
Hi Cori

I was using the PreRender event so as to ensure that all the controls had already been bound to their data sources.

Still not entirely sure what the problem was. Although, we have made a design decision to create datasources and bind our controls in code rather than using on page SqlDataSources. We are not having the problem doing it this way.

Shane
Tags
ComboBox
Asked by
Shane Clay
Top achievements
Rank 1
Answers by
Shane Clay
Top achievements
Rank 1
Cori
Top achievements
Rank 2
Share this question
or