Checkbox in a datagrid always returns false

Discussion in 'ASP .Net Web Controls' started by Prescott, Mar 21, 2005.

  1. Prescott

    Prescott Guest

    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 ...
     
    Prescott, Mar 21, 2005
    #1
    1. Advertising

  2. Prescott

    Teemu Keiski Guest

    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" <> wrote in message
    news:...
    > 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 ...
    >
     
    Teemu Keiski, Mar 22, 2005
    #2
    1. Advertising

  3. 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!
     
    Prescott Chartier, Mar 22, 2005
    #3
  4. 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!
     
    hitesh chawla, Mar 26, 2005
    #4
  5. 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!
     
    Prescott Chartier, Mar 29, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. feng

    IsPostBack always returns False

    feng, Dec 9, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    571
    Jose L Rodriguez
    Dec 9, 2003
  2. mehul
    Replies:
    0
    Views:
    767
    mehul
    May 3, 2004
  3. peter
    Replies:
    3
    Views:
    356
    peter
    Jun 22, 2006
  4. DJ
    Replies:
    3
    Views:
    932
  5. Iñaki Baz Castillo
    Replies:
    9
    Views:
    196
    Igor Pirnovar
    Feb 26, 2009
Loading...

Share This Page