2
votes

I have a repeater control and each item/row has an asp:Checkbox control and a button next to it.

My problem is that if I check a given checkbox and click on the button next to it [before postback], the state (checked) of the previously checked Checkbox is preserved [after postback]. But I don't want it to be preserved so I've done the following for all the Checkboxes:

  1. Disabled the viewstate (EnableViewState='false') of the Checkboxes (and repeater)
  2. Defined the property Checked="false"
<td>
<asp:CheckBox ID="chk_chooseTOL" CssClass="chk_chooseTOL" runat="server" Checked="false" EnableViewState="false" />
</td>

I looked at the state of the checkbox from the code-behind during the ItemDataBound event of the repeater in debugging mode, and it's fine, none of the checkboxes is checked, but at the end, the page is displayed and the previously checked checkbox remains checked. I don't understand why.

Do you have any idea how to not preserved the state of my checkboxes? (I need the check boxes to be server-side controls in order to disable them during the ItemDataBound event on a particular condition)

Thank you

Some more code :

<asp:Repeater ID="rpt_CA" runat="server" DataSourceID="TLDataSource" EnableViewState="false" OnItemDataBound="rpt_CA_OnItemDataBound" ClientIDMode="Static" >
    <HeaderTemplate>
        <table class="ca_table persist-area" id="rpt_CA">
            <thead>
            <tr class="tl-header persist-header">
                <th>Consultant</th>
                <th>Mode</th> 
                <th></th> 
            </tr>
            </thead>
            <tbody>
    </HeaderTemplate>
    <ItemTemplate>
    <tr>
        <td>
        <asp:Label runat="server" Text='<%# Eval("EMPLOYEES.FULLNAME") + " (" + Eval("ID_EMPLOYEE") + ")"  %>' />
        </td>
        <td>
        <asp:Button runat="server" Text="Gen" EnableViewState="false" />
        </td>
        <td>
        <asp:CheckBox ID="chk_chooseTOL" CssClass="chk_chooseTOL" runat="server" Checked="false" EnableViewState="false" />
        </td>
    </tr>
    </ItemTemplate>
    <FooterTemplate>
        </tbody>
        </table>
    </FooterTemplate>
</asp:Repeater>

.

protected void rpt_CA_OnItemDataBound(object Sender, RepeaterItemEventArgs Args) {
    if (Args.Item.ItemType == ListItemType.Item || Args.Item.ItemType == ListItemType.AlternatingItem)
    {   TURNOVER_LINES currentTL = (TURNOVER_LINES)Args.Item.DataItem;  RepeaterItem ri = Args.Item;

             CheckBox chk_chooseTOL = Args.Item.FindControl("chk_chooseTOL") as CheckBox;
             if(currentTL.IS_ALREADY_GEN)
             {
                 chk_chooseTOL.Enabled = false;
             }

    }
}
1
did you tried EnableViewState="False" at repeater level? - Prashant Lakhlani
Yes, the repeater has the property EnableViewState="False", thank you - TGI
Can you post some more asp code and code behind if any? - Prashant Lakhlani
Perhaps this could enlighten you better Repeaters and Lost Data After Postback (Viewstate)? - chridam
Are you binding your data in 'if(!IsPostBack) { }`? - Prashant Lakhlani

1 Answers

1
votes

It's not enough to put Checked="false" to lose the state. This value will be overwritten with the checked state in the form. To prevent that, you need to set checked=false from the code-behind, in the page_load event, or after it.