-
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
-
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
Forum Rules
|
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
|
Bookmarks