×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Detecting Checked Checkboxes in DataGrid

Detecting Checked Checkboxes in DataGrid

Detecting Checked Checkboxes in DataGrid

(OP)
Greetings,

I've been unable to determine which CheckBoxes are being 'checked' within a DataGrid.  The following code iterates through the entire datagrid, but never identifies the checked checkboxes.

CODE

Sub OnCheckOut( s As Object, e As EventArgs )
  Dim i As Integer
  Dim blnIsChecked As Boolean    
         
  For i = 0 to dgrd_EmailUpdate.Items.Count - 1
    blnIsChecked = DirectCast(dgrd_EmailUpdate.Items(i).Cells(4).FindControl("chkSelected"), Web.UI.WebControls.CheckBox).Checked            
      If blnIsChecked = True Then
                response.write( i & " IS Checked <BR>")
                response.write("<BR>")                
      Else
                response.write( i & " IsNotChecked ")
                response.write("<BR>")
      End If            
  Next       
End Sub


Here is the datagrid I'm using as well as the button to kick things off.

CODE

<asp:DataGrid ID="dgrd_EmailUpdate"
    AutoGenerateColumns="False" Runat="Server">        <Columns>
     <asp:BoundColumn DataField="ID" HeaderText="ID" />
     <asp:BoundColumn DataField="EmailAddress" HeaderText="Email Address" />
     <asp:BoundColumn DataField="IPaddress" HeaderText="IPAddress" />
    <asp:BoundColumn DataField="datetime" HeaderText="DateTime"    HeaderStyle-Width="100" />
    <asp:TemplateColumn HeaderText="Remove"
    <ItemTemplate>
         <TABLE>
        <TR>
             <TD align="center">
            <asp:CheckBox id="chkSelected"  runat="server" />            
             </TD>
        </TR>
         </TABLE>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>            
</asp:DataGrid>                        

<asp:Button Text="btnRemove" style="FONT:8pt Verdana; WIDTH: 100px" OnClick="OnCheckOut" RunAt="server" />            

Clearly I'm missing something, but obviously I'm not bright enough to figure it out.

I greatly appreciate any help you can provide.

thanks
        

RE: Detecting Checked Checkboxes in DataGrid

you could cast your control to a checkbox and test the the checked property.
never tried casting the value of a checkbox to a boolean

CODE

Sub OnCheckOut( s As Object, e As EventArgs )
  Dim i As DataGridItem
  For Each i In dgrd_EmailUpdate.Items
    Dim cb_Selected As CheckBox = CType(i.FindControl("chkSelected"), CheckBox)
      If cb_Selected.Checked Then
          response.write( i & " IS Checked <BR>")
          response.write("<BR>")                
      Else
          response.write( i & " IsNotChecked ")
          response.write("<BR>")
      End If            
  Next i       
End Sub
hth,
Marty

RE: Detecting Checked Checkboxes in DataGrid

In the debugger, does your code successfully find the CheckBox (though not its value)? Do you re-bind before checking values?

RE: Detecting Checked Checkboxes in DataGrid

(OP)
Yes, the checkbox is found.  Turns out that both options above work perfectly.  The only requirement (which I somehow overlooked multiple times) is that re-binding the datagrid NOT occur with each postback (duh!), so I had to include the binding in an "If NOT IsPostBack" condition within Page_Load().

Case closed!

Thanks

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close