Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Checkbox in a datagrid always returns false

Reply
Thread Tools

Checkbox in a datagrid always returns false

 
 
Prescott
Guest
Posts: n/a
 
      03-21-2005
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">
<aspataGrid 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>
</aspataGrid>
<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 ...

 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      03-22-2005
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.

--
Teemu Keiski
ASP.NET MVP, Finland


"Prescott" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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">
> <aspataGrid 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>
> </aspataGrid>
> <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 ...
>



 
Reply With Quote
 
 
 
 
Prescott Chartier
Guest
Posts: n/a
 
      03-22-2005
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 ...




*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
hitesh chawla
Guest
Posts: n/a
 
      03-26-2005
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...


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Prescott Chartier
Guest
Posts: n/a
 
      03-29-2005
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 ...

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"def nothing=(data) false end" returns 'data' instead of 'false' IƱaki Baz Castillo Ruby 9 02-26-2009 09:38 PM
CustomValidator Returns False, but False is Ignored DJ ASP .Net 3 12-27-2007 10:29 AM
False positive, false intrusion, false alarm Nick Computer Security 3 04-26-2006 07:40 PM
CheckBox template always evaluate to False even if checked in a DataGrid hosted inside a TabStrip mehul ASP .Net 0 05-03-2004 12:07 PM
IsPostBack always returns False feng ASP .Net 1 12-09-2003 04:19 PM



Advertisments