Need Help with Combo Box at Page Render
Hi,
I have combo box inside a data grid.
At page render event I want to have additional entry as "Select One..." with value -1 but display the existing old value as default value in drop down list.
To get this result first I create a object of ListItemCollection, I add entry as "Select One..." with value of -1 and then assign that ListItemCollection as a datasource to drop down list, but with this method I cant compare values because Values changes to Texts and both text/value pairs in list item have same values as that of list item text
I am using following code
Dim cmbSegmentation As DropDownList
Dim lstOldSegmentation As ListItem
If Not DataGrid1.Items(DataGrid1.SelectedIndex).FindControl("Segmentation") Is Nothing Then
cmbSegmentation = DataGrid1.Items(DataGrid1.SelectedIndex).FindControl("Segmentation")
lstOldSegmentation = cmbSegmentation.SelectedItem
Dim arr As New ListItemCollection
arr.Clear()
If cmbSegmentation.Items(0).Text = "Select One..." Then
Else
arr.Add(New ListItem("Select One...", "-1"))
End If
Dim I As Integer
For I = 0 To cmbSegmentation.Items.Count - 1
arr.Add(New ListItem(cmbSegmentation.Items(I).Text, cmbSegmentation.Items(I).Value))
Next
cmbSegmentation.Items.Clear()
cmbSegmentation.DataSource = arr
cmbSegmentation.DataBind()
cmbSegmentation.SelectedValue = lstOldSegmentation.Value
End If
Using this code, I am unable to set default value which was previously present in combo box. But when I tried using a For Loop to populate list items in cmbSegmentation, the code worked correctly and the default value or selected item is displayed in drop down list as expected.
My question is what is wrong with the above code?
Comments: if running a For Loop is only the remedy to such type of situation then the .NET speficied methods datasource, databind are of no use, we are landing at the same old ASP style programs.

