Solved

gridview checkbox cannot find those checked

Posted on 2010-11-19
3
422 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Events in static methods 3 58
vb.net checkbox 7 46
I need help viewing multiple page TIFF image in my WPF with VB.Net code-behind application 7 29
Variable Event ? 3 27
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

813 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