I have a ListView with LayoutTemplate, ItemTemplate and EditTemplate.
The listview is bound to a datatable in codebehind. The item template is made up of lables and checkboxes. The listview will only ever show one record. The listview works great when in ItemTemplate. On clicking the Edit button my ItemEditing method is invoked which has the following. BTW the EditTemplate has textboxes checkboxes and 4 dropdown lists.
Protected Sub ListView1_ItemEditing(ByVal sender As Object, ByVal e As ListViewEditEventArgs) Handles ListView1.ItemEditing
ListView1.EditIndex = e.NewEditIndex
'Create SQL, Execute and save to Datatable and bind to ListView1
'Session("SID") is the ID of the Subject we are viewing.
LoadData(Session("SID"))
End Sub
Unfortunately as the dropdownlist have lo ListItems I get an error that the binding failed as the value did not exist in the dropdownlist.
So I tried to populate my dropdownlists when ItemCreated… My code is…
Protected Sub ListView1_ItemCreated(ByVal sender As Object, ByVal e As ListViewItemEventArgs) Handles ListView1.ItemCreated
Try
Dim ddl_Type As DropDownList = ListView1.EditItem.FindControl("ddl_Type")
Dim ddl_Suitability As DropDownList = ListView1.EditItem.FindControl("ddl_Suitability")
Dim ddl_Brands As DropDownList = ListView1.EditItem.FindControl("ddl_Brands")
Dim ddl_Status As DropDownList = ListView1.EditItem.FindControl("ddl_Status")
'We need to populate ddls
SqlStr = "SELECT Type As ddlText FROM tbl_SType ORDER BY Type"
PopulateDDL(ddl_Type, SqlStr, "Please Select...")
SqlStr = "SELECT Suitability As ddlText FROM tbl_Suitability ORDER BY Suitability"
PopulateDDL(ddl_Suitability, SqlStr, "Please Select...")
SqlStr = "SELECT Brand As ddlText FROM tbl_Brands ORDER By Brand"
PopulateDDL(ddl_Brands, SqlStr, "Please Select...", "Other...")
SqlStr = "SELECT StatusText As ddlText, Status As ddlValue FROM tbl_Status ORDER BY Status"
PopulateDDLvalue(ddl_Status, SqlStr, "Not Visited Yet", "0")
Catch ex As Exception
End Try
End Sub
But the controls are not being found by FindControl. Any Ideas on where I’m going wrong?
As a workarund, you can leave the databinding in the ItemDataBound event, and set the selected values of the drop downs programmatically. It’s a pain I know, but it would solve the problem you are experiencing.