Trying to set the selected item in a dropdown list programmatically


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Trying to set the selected item in a dropdown list programmatically

  1. #1
    Rodney Hall Guest

    Trying to set the selected item in a dropdown list programmatically


    Hi,

    I'm trying to set the selected item in a dropdown list programmatically,
    the dropdown list is bound to a dataset. However, the selectedIndex is read
    only. Is there any other way of doing this, if I did a find on the bound
    dataset would this synch the dropdown list? Any help would be appreciated.

    Thanks,

    Rodney Hall

  2. #2
    Greg Rothlander Guest

    Re: Trying to set the selected item in a dropdown list programmatically



    This can be a little tricky depending on where your dropdownlist is located.
    If it's in a datalist or datagrid's edititem template then it can be hard
    to code, something like the will work. My dropdownlist has an id of "ddlCardType"

    Private Sub datalist1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)
    Handles datalist1.ItemDataBound

    If e.Item.ItemType = ListItemType.EditItem Then

    Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
    Dim strType As String = CType(drv("Credit_CardType"), String)

    Dim ddlControl As DropDownList
    ddlControl = CType(e.Item.FindControl("ddlCardType"), DropDownList)
    ddlControl.SelectedIndex = ddlControl.Items.IndexOf(ddlControl.Items.FindByText(strType))

    End If

    End Sub

    If you simply have a dropdownlist on a form, something like the following
    would work. lstTitle in the id of the dropdownlist.

    'lstTitle
    nodeXML = xDoc.SelectSingleNode(strXMLrootpath & "Title") 'Get
    the value from an XMLDocument
    Dim t As Integer
    For t = 0 To (lstTitle.Items.Count - 1)
    If lstTitle.Items(t).Value.Equals(nodeXML.InnerText) Then
    lstTitle.SelectedIndex = t
    End If
    Next

    This is probably the simplest way to handle this, but there are problems
    using this inside the datalist, datagrid, etc. The previous code will handle
    that for you.

    Best Regards,
    Greg Rothlander
    Austin/400
    grothlander@austin400.com

    "Rodney Hall" <rhall@creditunion.mb.ca> wrote:
    >
    >Hi,
    >
    >I'm trying to set the selected item in a dropdown list programmatically,
    >the dropdown list is bound to a dataset. However, the selectedIndex is

    read
    >only. Is there any other way of doing this, if I did a find on the bound
    >dataset would this synch the dropdown list? Any help would be appreciated.
    >
    >Thanks,
    >
    >Rodney Hall



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center