Search code examples
asp.netrepeateritemtemplate

How do you disable a control within a repeater


I have a repeater with a CheckBox and a TextBox, that I am binding to a data source. Depending on conditions from the code behind, I want to be able to disable the CheckBox and TextBox dynamically.

Currently this is what I have within the <ItemTemplate>:

<td>
    <asp:CheckBox runat="server" 
        onclick="checkbox(this); CheckChildren(this);" 
        Enabled='<%#DataBinder.Eval(Container.DataItem, "DISABLE") %>' 
        Text='<%#DataBinder.Eval(Container.DataItem, "CTEXT") %>' 
        Value='<%#DataBinder.Eval(Container.DataItem, "CVALUE") %>' />
</td>

<td>
    <asp:TextBox runat="server" 
        onkeyup="AppendValues(this);" 
        Enabled='<%#DataBinder.Eval(Container.DataItem, "DISABLE") %>' 
        Width="35px" 
        MaxLength="3" 
        Name='<%#DataBinder.Eval(Container.DataItem, "CNAME") %>' 
        CValue='<%3DataBinder.Eval(Container.DataItem, "CNTVALUE") %>'>
    </asp:TextBox>
</td>

The #DataBinder.Eval(Container.DataItem, "DISABLE") is being set to the string value of "true" on the code behind yet when the code is ran, I receive an InvalidCastException exception.

What am I doing wrong?


Solution

  • You need to pass a boolean value to the Enabled property. Try something like this

    bool.Parse(DataBinder.Eval(Container.DataItem, "DISABLE").ToString())