FindItemByValue not selecting correctly

4 posts, 0 answers
  1. Shane Clay
    Shane Clay avatar
    29 posts
    Member since:
    May 2010

    Posted 12 Oct 2010 Link to this post

    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
  2. Shane Clay
    Shane Clay avatar
    29 posts
    Member since:
    May 2010

    Posted 12 Oct 2010 Link to this post

    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
  3. Cori
    Cori avatar
    562 posts
    Member since:
    Jul 2010

    Posted 13 Oct 2010 Link to this post

    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.
  4. Shane Clay
    Shane Clay avatar
    29 posts
    Member since:
    May 2010

    Posted 13 Oct 2010 Link to this post

    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
Back to Top