|
-
Apr 20th, 2023, 10:25 PM
#1
[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?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 21st, 2023, 03:11 AM
#2
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.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Apr 21st, 2023, 03:26 AM
#3
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…👍
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 22nd, 2023, 11:09 AM
#4
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|