Results 1 to 4 of 4

Thread: [RESOLVED] Webforms DataGrid CheckBox problem

  1. #1

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Resolved [RESOLVED] Webforms DataGrid CheckBox problem

    I'm working on a Webforms app, with CSS tabpages. Each tabpage contains a game. Due to the nature of the games, i have to force postbacks at various points in the app. Two of the apps have an asp:datagrid, both with a checkbox column and a text column. When i postback, the text list is retained, but the checkboxes all return to false. These checkboxes are purely for visual purposes. They have no real effect on the app, other than as a courtesy for the player to keep track of things. This is the markup...

    Code:
    <asp:DataGrid ID="firstList" runat="server" CssClass="setlistbox" AutoGenerateColumns="false">
        <Columns>
            <asp:TemplateColumn HeaderText="none1" ItemStyle-Width="20">
                <ItemTemplate>
                    <asp:CheckBox runat="server" ID="chkSelect" Checked='<%#Eval("Selecter").ToString() %>' />
                </ItemTemplate>
            </asp:TemplateColumn>
    
            <asp:BoundColumn HeaderText="none2" DataField="TextItem"></asp:BoundColumn>
    
        </Columns>
    </asp:DataGrid>
    This is how i bind it...

    Code:
    dt1 = New DataTable
    dt1.Columns.Add("Selecter", GetType(Boolean))
    dt1.Columns.Add("TextItem")
    
    dt1.Rows.Add(False, "example")
                    
    firstList.DataSource = dt1
    firstList.DataBind()
    I tried looping through the items server side...

    Code:
    For Each item As DataGridItem In firstList.Items
        Dim chkBox As CheckBox = DirectCast(item.FindControl("chkSelect"), CheckBox)
        Debug.Print(chkBox.Checked.ToString)
        dt1.Rows(item.ItemIndex).ItemArray(0) = chkBox.Checked ' this doesn't save the checkboxes' state
    Next
    This prints out the correct values, but doesn't save the checkboxes' state

    Can anyone help with this?

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Webforms DataGrid CheckBox problem

    Again, looot of years to asp.net but some thoughts.
    If they return to their initial state, try setting the page.ispostback property, maybe they refresh to their initial state.
    The second option is another way around that you will postback with p.e. Ajax and do the check with javascript. That would need re-engineering everything so I'm not sure if you want to do so but none of those troublesome postbacks.

    Just for heads up here is how you would go about checking every checkbox in a grid with javascript.
    https://www.vbforums.com/showthread....ith-Javascript
    Last edited by sapator; Apr 21st, 2023 at 04:27 AM.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  3. #3

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Webforms DataGrid CheckBox problem

    Thanks. I need to keep it all on the server side, because of the binding. I had another think about it, and when I get back to it, I’m going to try creating a new DataTable, containing the correct values and re-binding. It’s such a simple problem, should be a simple fix😎 Well, I would’ve thought it would be…👍

  4. #4

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Webforms DataGrid CheckBox problem

    Resolved at last. I forget that asp.net is different to winforms, and reactions to errors manifest themselves differently in the app. It’s illogical

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width