Magicamente ho risolto il problema sostituendo dg_abilitati.load con dg_abilitati.Init
ecco il codice
codice:
Public Class adm_abilitazione_professionisti_area_clienti
Inherits Page
'Private conn As New connessione_db
Protected WithEvents dg_abilitati As DataGrid
Protected WithEvents lb_aggiorna As LinkButton
Protected txb_avviso As Label
Private ds As New DataTable
Dim constring As String = "DSN=dyEngV2"
Dim connessione As New OdbcConnection(constring)
Dim adp As New Odbc.OdbcDataAdapter
Protected Sub Page_Load() Handles Me.Load
If Session("granted") = False Then
Server.Transfer("login.aspx")
End If
If Me.IsPostBack = False Then
txb_avviso.Visible = False
End If
End Sub
Protected Sub Popola_dg_abilitati() Handles dg_abilitati.Init
' conn.query = "Select * from utenti_registrati where idsite='" & Session("idsite") & "' and azienda<>''"
'Dim ds As DataSet = conn.esegui
adp.SelectCommand = New OdbcCommand("Select * from utenti_registrati where idsite='" & Session("idsite") & "' and azienda<>''")
adp.SelectCommand.Connection = connessione
Dim cb As New OdbcCommandBuilder(adp)
adp.Fill(ds)
dg_abilitati.DataSource = ds
dg_abilitati.DataKeyField = "id"
dg_abilitati.DataBind()
End Sub
Protected Sub Aggiorna_Dati() Handles lb_aggiorna.Click
For Each row As DataGridItem In dg_abilitati.Items
If row.ItemType = ListItemType.AlternatingItem Or row.ItemType = ListItemType.Item Then
Dim ckb As CheckBox = row.Cells(5).FindControl("ckb_abilitato")
If ckb.Checked = True Then
ds.Rows(row.ItemIndex)("abilitato") = "1"
Else
ds.Rows(row.ItemIndex)("abilitato") = "0"
End If
End If
Next
adp.Update(ds)
End Sub
Protected Sub Dg_abilitati_DataBound(ByVal sender As DataGrid, ByVal e As DataGridItemEventArgs) Handles dg_abilitati.ItemCreated
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim ckb As CheckBox = e.Item.Cells(5).FindControl("ckb_abilitato")
If ds.Rows(e.Item.ItemIndex)("abilitato") = "1" Then
ckb.Checked = True
Else
ckb.Checked = False
End If
End If
End Sub
End Class
Esiste un modo per fare le cose più pulite e con meno codice?? Qual'è la regola darte da seguire per questo esempio??