Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

SelectedIndex of DropDownList inside of a Repeater

I have a dropdownlist which lives inside of a repeater. The repeater accepts user input via textboxes and the aforementioned dropdownlist.
The repeater accepts phone numbers and allows for an arbitrary number of empty rows to be added to it.
So, if a user knew in advance that they wanted to add 3 phone numbers, they can type the nuber '3' in a textbox (outside the repeater)
and click a button (also outside of a repeater), and 3 new empty  rows will appear in the repeater.  The code that does that looks
like this:

For Each objRepeaterItem As RepeaterItem In rptAgencyPhoneList.Items
    Dim ddlPhoneCategory As DropDownList = DirectCast(objRepeaterItem.FindControl("ddlPhoneCategory"), DropDownList)
    'Create a Phone object
        Dim objPhone As New Phone(ConvertExpectedInteger(DirectCast(objDataGridItem.FindControl("lblPkPhone"), Label).Text))
        With objPhone
          .Number = DirectCast(objDataGridItem.FindControl("txtAdditionalVoice"), TextBox).Text
          .TTYNumber = DirectCast(objDataGridItem.FindControl("txtAdditionalTTY"), TextBox).Text
          .Category = ConvertExpectedInteger(ddlPhoneCategory.SelectedValue)
          .CategoryName = ConvertExpectedString(ddlPhoneCategory.SelectedItem.Text)
        End With
    'Add the existing items to the collection

     'Add the requested blank rows to the collection
        For i As Integer = 1 To rowsToAdd
              Dim objPhone As New Phone

After I have added the information from the repeater into a collection, I bind to the repeater
rptAgencyPhoneList.dataSource = objPhoneCollection

Then the repeater's item_created event takes over, as shown below.

'eliminate headers, footers, seperators....
    If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
    'Populate the phone category drop down list
          Dim ddlPhoneCategory As DropDownList = DirectCast(e.Item.FindControl("ddlPhoneCategory"), DropDownList)
         Dim objPhoneCategoryList As ListItemCollection = getPhoneCategoryList()
          For Each item As ListItem In objPhoneCategoryList

    'If a phone number exists, then select the appropriate category type in the drop down list
         If ConvertExpectedInteger(DataBinder.Eval(e.Item.DataItem, "Category")) > 0 Then
            ddlPhoneCategory.Items.FindByValue(DataBinder.Eval(e.Item.DataItem, "Category").ToString).Selected = True
         End If
    End If

After all of this is finished, the page is displayed. However the dropdownlist's selected item is always set to the first item in the list
no matter what. Also, If I add an Else statement to the last IF..End IF a error message is thrown stating that a dropdownlist can have
only one selected item.

Does anyone have any ideas?

Dim ddlPhoneCategory As DropDownList = DirectCast(objRepeaterItem.FindControl("ddlPhoneCategory"), DropDownList)

Tuesday, May 17, 2005

something like

    ddlPhoneCategory.SelectedIndex =
ddlPhoneCategory.Items.IndexOf(ddlPhoneCategory.Items.FindByValue(DataBinder.Eval(e.Item.DataItem, "Category").ToString))

Wednesday, May 18, 2005

Thanks, Aidan.  I tried the syntax that you offered and it still does not select an item from the drop down list. Is there any reason why the selectedIndex property would be ignored?

Wednesday, May 18, 2005

I may have meant "FindByText" rather than find by item.

response.write() various bits of the statement to see if you are getting the right sort of thing.

Experiment with

ddlPhoneCategory.SelectedIndex = 2

to see if setting the index actually sets what is shown on the screen (1st item in list is index = 0)

it could be that some other binding event that comes later is re-setting the dropdown.

It may be that some people know the framework well enough to know if things like dropdowns in repeaters in datagrids are going to work... but I am not one of them and end up doing a lot of experimenting as i code :)

Thursday, May 19, 2005

*  Recent Topics

*  Fog Creek Home