Return to Snippet

Revision: 34777
at October 28, 2010 04:48 by derebus


Updated Code
Protected Sub chkResponsable_OnCheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim chkResponsable As CheckBox = CType(sender, CheckBox)
        Dim dtResponsable As DataTable = Session("dtResponsable")
        Dim idResponsable As Integer

        'Navega por la gerarquia de controles que contienen al CheckBox hasta llegar al GridViewRow
        Dim gvrFilaActual As GridViewRow = DirectCast(DirectCast(chkResponsable.Parent, DataControlFieldCell).Parent, GridViewRow)
        'Obtiene el campo idResponsable que esta en la fila del CheckBox que se ha seleccionado y actualiza su estado bool
        idResponsable = DirectCast(gvrFilaActual.Cells(2).FindControl("lblidResponsable"), Label).Text

        If chkResponsable.Checked = True Then
            For i = 0 To dtResponsable.Rows.Count - 1
                If dtResponsable.Rows(i)("idResponsable") = idResponsable Then
                    dtResponsable.Rows(i)("valorBool") = 1
                End If
            Next
        Else
            For i = 0 To dtResponsable.Rows.Count - 1
                If dtResponsable.Rows(i)("idResponsable") = idResponsable Then
                    dtResponsable.Rows(i)("valorBool") = 0
                End If
            Next
        End If
        Session("dtResponsable") = dtResponsable

        lblConfirma.Text = ""
        lblError.Text = ""
    End Sub

        '<asp:GridView ....>
        '    <Columns>
        '        ....
        '        <asp:TemplateField HeaderText="Seleccionar">
        '            <ItemTemplate>
        '                <asp:Checkbox ID="chkResponsable" runat="server" AutoPostBack="true" Checked='<%# Eval("valorBool") %>' OnCheckedChanged="chkResponsable_OnCheckedChanged" />
        '            </ItemTemplate>
        '            <HeaderStyle HorizontalAlign="Center" Width="5%" />
        '            <ItemStyle HorizontalAlign="Center" Width="5%" />
        '        </asp:TemplateField>                                                                                                                                                                                     
        '    </Columns>
        '</asp:GridView>

Revision: 34776
at October 28, 2010 04:48 by derebus


Updated Code
Protected Sub chkResponsable_OnCheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim chkResponsable As CheckBox = CType(sender, CheckBox)
        Dim dtResponsable As DataTable = Session("dtResponsable")
        Dim idResponsable As Integer

        'Navega por la gerarquia de controles que contienen al CheckBox hasta llegar al GridViewRow
        Dim gvrFilaActual As GridViewRow = DirectCast(DirectCast(chkResponsable.Parent, DataControlFieldCell).Parent, GridViewRow)
        'Obtiene el campo idResponsable que esta en la fila del CheckBox que se ha seleccionado y actualiza su estado bool
        idResponsable = DirectCast(gvrFilaActual.Cells(2).FindControl("lblidResponsable"), Label).Text

        If chkResponsable.Checked = True Then
            For i = 0 To dtResponsable.Rows.Count - 1
                If dtResponsable.Rows(i)("idResponsable") = idResponsable Then
                    dtResponsable.Rows(i)("valorBool") = 1
                End If
            Next
        Else
            For i = 0 To dtResponsable.Rows.Count - 1
                If dtResponsable.Rows(i)("idResponsable") = idResponsable Then
                    dtResponsable.Rows(i)("valorBool") = 0
                End If
            Next
        End If
        Session("dtResponsable") = dtResponsable

        lblConfirma.Text = ""
        lblError.Text = ""
    End Sub

'<asp:GridView ....>
        '    <Columns>
        '        ....
        '        <asp:TemplateField HeaderText="Seleccionar">
        '            <ItemTemplate>
        '                <asp:Checkbox ID="chkResponsable" runat="server" AutoPostBack="true" Checked='<%# Eval("valorBool") %>' OnCheckedChanged="chkResponsable_OnCheckedChanged" />
        '            </ItemTemplate>
        '            <HeaderStyle HorizontalAlign="Center" Width="5%" />
        '            <ItemStyle HorizontalAlign="Center" Width="5%" />
        '        </asp:TemplateField>                                                                                                                                                                                     
        '    </Columns>
        '</asp:GridView>

Revision: 34775
at October 28, 2010 04:45 by derebus


Initial Code
Protected Sub chkResponsable_OnCheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim chkResponsable As CheckBox = CType(sender, CheckBox)
        Dim dtResponsable As DataTable = Session("dtResponsable")
        Dim idResponsable As Integer

        'Navega por la gerarquia de controles que contienen al CheckBox hasta llegar al GridViewRow
        Dim gvrFilaActual As GridViewRow = DirectCast(DirectCast(chkResponsable.Parent, DataControlFieldCell).Parent, GridViewRow)
        'Obtiene el campo idResponsable que esta en la fila del CheckBox que se ha seleccionado y actualiza su estado bool
        idResponsable = DirectCast(gvrFilaActual.Cells(2).FindControl("lblidResponsable"), Label).Text

        If chkResponsable.Checked = True Then
            For i = 0 To dtResponsable.Rows.Count - 1
                If dtResponsable.Rows(i)("idResponsable") = idResponsable Then
                    dtResponsable.Rows(i)("valorBool") = 1
                End If
            Next
        Else
            For i = 0 To dtResponsable.Rows.Count - 1
                If dtResponsable.Rows(i)("idResponsable") = idResponsable Then
                    dtResponsable.Rows(i)("valorBool") = 0
                End If
            Next
        End If
        Session("dtResponsable") = dtResponsable

        lblConfirma.Text = ""
        lblError.Text = ""
    End Sub

Initial URL
http://foros.elguille.info/Mensajes.aspx?ID=34315

Initial Description
Al hacer click en el checkbox que se encuentra dentro del gridview, capturo el id unico de quien pertenece dicho checkbox y guardo los cambios en el datatable.

Initial Title
Checkbox dentro de Gridview

Initial Tags


Initial Language
VB.NET