Solved

gridview checkbox cannot find those checked

Posted on 2010-11-19
3
413 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
Comment Utility
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
Comment Utility
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
Comment Utility
- 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

12 Experts available now in Live!

Get 1:1 Help Now