I have a page that would dynamically populate a checkboxlist from database. And there is a gridview with a column that would store selected value of the checkboxlist. So, when a row is selected, the checkboxlist would be checked with the value from gridview. My problem is, if the checkboxlist is not dynamically populated, the checkboxlist would be checked with the value in gridview but not a dynamically populated checkboxlist. And i'm using asynchronous connection to retrieve data from DB.
My code as below:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dt As DataTable = New DataTable
Try
If Not IsPostBack Then
Dim cnnStr As String = DBMgr1.asyncADOCnnStr("SQLDS")
Dim cnn As SqlConnection = New SqlConnection(cnnStr)
cnn.Open()
Dim ProdCodeStr As String = "SELECT ProdCode " & _
"FROM Products_Mapping " & _
"WHERE ProductType = 'UPL'"
Dim cmdProdCode As New SqlCommand(ProdCodeStr, cnn)
cmdProdCode.CommandType = CommandType.Text
Dim result As IAsyncResult = cmdProdCode.BeginExecuteReader()
Dim dr As SqlDataReader = cmdProdCode.EndExecuteReader(result)
dt.Load(dr)
Dim ListItemArray(dt.Rows.Count) As ListItem
ListItemArray(0) = New ListItem
ListItemArray(0).Text = "ALL".ToString
ListItemArray(0).Value = "ALL".ToString
For i As Integer = 1 To dt.Rows.Count
ListItemArray(i) = New ListItem
ListItemArray(i).Text = dt.Rows(i - 1)("ProdCode").ToString
ListItemArray(i).Value = dt.Rows(i - 1)("ProdCode").ToString
Next
chkProdCode.Items.AddRange(ListItemArray)
dr.Close()
cnn.Close()
dt = Nothing
End If
Catch ex As Exception
MSGMgr.errHandlerSys(ex.Message, lblMsg)
End Try
End Sub
Protected Sub gv_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles gv.SelectedIndexChanged
CVRMgr = New cConverter
Try
If Not bIsRefresh Then
.....
If Not gv.DataKeys(gv.SelectedIndex).Values("ProdCode").ToString.Trim Is Nothing Then
Dim prodCode() As String
Dim prodCodeValue As String = gv.DataKeys(gv.SelectedIndex).Values("ProdCode").ToString.Trim
prodCode = prodCodeValue.Split(",")
For i As Integer = 0 To prodCode.Count - 1
Dim li As ListItem = chkProdCode.Items.FindByValue(prodCode(i))
If Not li Is Nothing Then
li.Selected = True
End If
Next
End If
.....
End If
Catch ex As Exception
MSGMgr.errHandlerSys(ex.Message, lblMsg)
End Try