Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detecting Checked Checkboxes in DataGrid

Status
Not open for further replies.

Sheffield

Programmer
Jun 1, 2001
180
0
0
US
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:)
 
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
 
In the debugger, does your code successfully find the CheckBox (though not its value)? Do you re-bind before checking values?
 
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:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top