Trying to set the selected item in a dropdown list programmatically
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.
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)
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))
If you simply have a dropdownlist on a form, something like the following
would work. lstTitle in the id of the dropdownlist.
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
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.
"Rodney Hall" <email@example.com> wrote:
>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
>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.
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL