开发者

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:

  1. Add a Hidden Field to the Template Field,
  2. Update the value of the hidden field to True or False with Javascript (adding an onclick attribute to the checkbox),
  3. 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜