Solved

gridview checkbox cannot find those checked

Posted on 2010-11-19
3
435 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 36

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

728 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