Link to home
Start Free TrialLog in
Avatar of bubblebob
bubblebob

asked on

gridview checkbox cannot find those checked

Have a gridview and checkbox bbb
when pressing update button, code goes through controls(pass isnot nothing test) but never finds a checked checkbox but I have checked a few.  Ideas  ???

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    BackColor="White" BorderColor="#336699" BorderStyle="Solid" BorderWidth="1px"  
    CellPadding="0" CellSpacing="0" Font-Size="10"
    Font-Names="Arial" GridLines="Vertical" Width="100%">
           
            <Columns>            
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="bbb" runat="server" Headertext="Authorise"/>
                    </ItemTemplate>                    
                </asp:TemplateField>
                <asp:BoundField DataField="Region" HeaderText="Region" />                    
                <asp:BoundField DataField="Authorised" HeaderText="Authorised Date"  />
                <asp:BoundField DataField="Payee" HeaderText="Payee"  />
                <asp:BoundField DataField="Amount" HeaderText="Amount"  />                
                <asp:BoundField DataField="AmountVAT" HeaderText="Amount VAT"  />                
            </Columns>
</asp:GridView>
    <asp:Button ID="btnTicked" runat="server" OnClick="btnTicked_Click" Text="Save ticked items" />

 Public Sub btnTicked_Click(ByVal sender As Object, ByVal e As EventArgs)
        For i As Integer = 0 To GridView1.Rows.Count - 1
            Dim chkDelete As CheckBox = DirectCast(GridView1.Rows(i).Cells(0).FindControl("bbb"), CheckBox)
            If chkDelete IsNot Nothing Then
                If (chkDelete.Checked = True) Then
                            LblStatus.Text = "yes"
               End If
            End If
        Next
end sub
Avatar of lucius_the
lucius_the
Flag of Croatia image

Gridvew is actually just s view component. What you huve underneath is a datatable. Some field in that datatable is boolean and is represented on gridview as a checkbox.

What you have to do is check the values in a datatable. You can reference the datatable with:
GridView1.Datasource, or better with ctype(GridView1.Datasource, DataTable), if datatable is used (generally it is).
ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bubblebob
bubblebob

ASKER

- Checkbox has no field in database, hence the use of templatefield.

- EnableViewState made no difference.

- Binding works as data shows and is extractable, just the checkbox value can't be seen.

- When you mention binding though - look at this, spot the obvious mistake ?

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        sSortBy$ = "CrChequeNo"
        sOrderBy$ = "DESC"
        sAuthOnly$ = "unauthorised"
        sChequeNo$ = ""
        Loaddata()

    End Sub
 Protected Sub Loaddata()
        GridView1.DataSource = clsConnection.Get_Cheques(sChequeNo, sSortBy, sOrderBy, sAuthOnly)
        GridView1.DataBind()
    End Sub

YES - oops, doing loaddata all the time.
Correct to :-
        If Not IsPostBack Then
            Loaddata()
        Else

        End If
Works fine.  Thanks