Checkbox in a datagrid always returns false

P

Prescott

Ok .. I've been beating my head against the wall for a week now, but no
matter what I do the checkbox is always false. I create a datagrid,
create a template column (all at design time) and add a checkbox
column. The code is below:

******** Form Code ******
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="Check50s.aspx.vb" Inherits="FileUpload.Check50s"%>

<%@ Register TagPrefix="uc1" TagName="Navigation" Src="Navigation.ascx"
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>Check50s</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 88px; POSITION:
absolute; TOP: 240px"
runat="server" Width="848px" Height="120px">
<Columns>
<asp:TemplateColumn HeaderText="Select Corrupt 50's">
<ItemTemplate>
<asp:CheckBox id="Ok" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<asp:Button id="btnSave" style="Z-INDEX: 102; LEFT: 88px; POSITION:
absolute; TOP: 192px" runat="server"
Width="120px" Height="32px" Text="Save 50's"></asp:Button>
<uc1:Navigation id="Navigation1" runat="server"></uc1:Navigation>
</form>
</body>
</HTML>

****** Code behind *******
Private Sub btnSave_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSave.Click

Dim DGI As DataGridItem
Dim TheDir As String = Server.MapPath("SplitFiles")
Dim FileLocation As String
Dim Ssn As String
Dim NoaCode As String
Dim NoaEffDate As String
OraConnection = OpenConnection()


For Each DGI In DataGrid1.Items
Dim CB As Checkbox = _
CType(DataGrid1.Items(0).Cells(0).Controls(1),_
CheckBox)
If Not CB.Checked Then
FileLocation = TheDir & "\" & DGI.Cells(1).Text
Ssn = DGI.Cells(2).Text
NoaCode = DGI.Cells(4).Text
NoaEffDate = DGI.Cells(5).Text
' Store data in database
StoreFile(OraConnection, FileLocation, Ssn _
, NoaCode, NoaEffDate)
File.Delete(FileLocation)
End If
Next

End Sub

What I'm looking for the user to do is mark those rows where the data
is "corrupt" by checking the checkbox and then click the "Save" button.
Problem is, I can't tell which rows the user selects because the
checkbox always returns false. If anyone could help me, it would be
greatly appreciated. Thanks,

Prescott ...
 
T

Teemu Keiski

Hi,

I can't see the code for binding the grid initially. Is it in Page_Load, and
do you check for IsPostBack in it? That is, make sure you don't rebind the
grid on postback when saving, because rebinding would reset the state of
CheckBoxes in DataGrid. If it happens unwanted, results would be what you
see now.
 
P

Prescott Chartier

Some days I feel like a total moron ... yep, that was exactly it ...
re-binding the control on postback. Damm ... a whole week. Sigh ....

Prescott ...
 
H

hitesh chawla

hi,
i am new to asp.net and C#...i read your solusion but i am using
C#..... i am having the same problem...the checkbox always returns
false..cud you plz tell me wht am i suppose to do..plz help...
 
P

Prescott Chartier

For me the solution was simple. In the Page Load event, I need only
load the datagrid once, on the initial page load. On postback you don't
want to re-load the datagrid because you then are re-initializing the
checkboxs, which will then return False everytime. I don't code C#, but
in VB the code is:

Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

If NOT IsPostBack Then
MyDataGrid.DataSource = CreateTable(DataArray)
MyDataGrid.DataBind()
MyDataGrid.Visible = True
End If

End Sub

This code preserves the state of the datagrid on postback.

Prescott ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top