Solved

gridview checkbox cannot find those checked

Posted on 2010-11-19
3
418 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:bubblebob
3 Comments
 
LVL 10

Expert Comment

by:lucius_the
ID: 34174857
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).
0
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 34176871
HOw is the binding done? Please post markup/code.
Does page has EnableViewState = true?

Do your findcontrol at row level:
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).FindControl("bbb"), CheckBox)
            If chkDelete IsNot Nothing Then
                If (chkDelete.Checked = True) Then
                            LblStatus.Text = "yes"
               End If
            End If
        Next
end sub
0
 

Author Closing Comment

by:bubblebob
ID: 34186566
- 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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now