Asp.Net Toggle checked property of a Data Bound checkbox
I have a GridView control that has one column of checkboxes set up like this:
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="RowCheckBox" runat="server" Enabled="false" Checked='<%# (bool)DataBinder.Eval(Container.DataItem ,"Associated") %>'/>
</ItemTemplate>
</asp:TemplateField>
Then when the user clicks an Edit button I run a script that enables all the checkboxes (which works fine), and then when the user then clicks on a checkbox the tick is appearing or disappearing as it should.
The problem I'm having is that when I try to read the value of the checkbox from the codebehind:
Chec开发者_开发百科kBox checkBox = (CheckBox) row.FindControl("RowCheckBox");
bool checked = checkBox.Checked;
If the value bound to it was true
then checked
is still true
, no matter if it was toggled or not.
Has anyone got any ideas why this is?
In the end I couldn't find a solution so have done a work around:
- Add a Hidden Field to the Template Field,
- Update the value of the hidden field to
True
orFalse
with Javascript (adding anonclick
attribute to the checkbox), - In the code behind cast the hidden field
value
to a bool and use that instead of the checked property of the checkbox.
I think it might be something to do with View States, but I'm not sure what they are and haven't got the time to investigate unfortunately.
Clivest
精彩评论