Setting SelectedValue for nested DropDownLists in DataList

Discussion in 'ASP .Net' started by Crazy Cat, Sep 10, 2007.

  1. Crazy Cat

    Crazy Cat Guest

    HI,

    I have a dropdownlist nested in a datalist.

    In the datalist's ItemDataBound event I populate the dropdownlist and
    set it's SelectedValue.
    I am finding that instead of each DropDownList being set to
    independent values all the dropdownlists' selectedvalue settings are
    being set to the value of the last dropdownlist set. Here's the code
    --

    Protected Sub dlIncentives_ItemDataBound(ByVal sender As Object,
    ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles
    dlIncentives.ItemDataBound

    If Not IsPostBack Then

    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType
    = ListItemType.AlternatingItem Then

    Dim ddlImportance As DropDownList =
    e.Item.FindControl("ddlImportance")
    For Each item As ListItem In Importances
    ddlImportance.Items.Add(item)
    Next

    ddlImportance.SelectedValue = CType(e.Item.DataItem,
    DataRowView).Row.Item("IncImp_ID").ToString

    End If

    End If

    End Sub

    Any idea what I could be doing wrong? I've set a breakpoint and can
    see that different values are being set for the SelectedValue, but
    when the page is displayed only the last value set is applied to the
    SelectedValue for all the dropdownlists.

    Thanks
     
    Crazy Cat, Sep 10, 2007
    #1
    1. Advertisements

  2. Crazy Cat

    bruce barker Guest

    because you add the same items to each list, they all behave the same.
    setting a item to selected set it in all dropdowns becuase its the same
    item. your code shoudl create new items for each dropdown.

    -- bruce (sqlwork.com)
     
    bruce barker, Sep 10, 2007
    #2
    1. Advertisements

  3. Crazy Cat

    Crazy Cat Guest

    Made the change you suggest and it works like a charm. I guess I
    didn't realize that under the covers setting SelectedValue actually
    set the Selected value of the underlying listitem.

    Thanks much Bruce.

    I rewrote the code thusly --


    Protected Sub dlIncentives_ItemDataBound(ByVal sender As Object,
    ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles
    dlIncentives.ItemDataBound

    If Not IsPostBack Then

    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType
    = ListItemType.AlternatingItem Then

    Dim ddlImportance As DropDownList =
    e.Item.FindControl("ddlImportance")
    For Each item As ListItem In Importances
    ddlImportance.Items.Add(New ListItem(item.Text,
    item.Value))
    Next

    ddlImportance.SelectedValue = CType(e.Item.DataItem,
    DataRowView).Row.Item("IncImp_ID").ToString

    End If

    End If

    End Sub
     
    Crazy Cat, Sep 10, 2007
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.