0
votes

I have this DetailsView component with several TemplateFields like this one:

<asp:TemplateField HeaderText="Eindtijd:">
    <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Bind("AVEITD") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="edtAVEITD" runat="server" Font-Size="Small" Font-Names="Arial"></asp:TextBox>

    </EditItemTemplate>
    <ControlStyle BackColor="#e9e015" Width="35px" />
</asp:TemplateField>

This results in a white "cell" in the table, with part of it taken by a editable TextBox with a yellow background, and exactly what I want.

However, in some cases, business logic says that the cell is NOT editable. So I set the Enabled property to false (works fine), but I also want to revert to a white background. And that's the problem.

Changing the BackColor of the Textbox doesn't work:

        tb = (TextBox) DetailsView2.FindControl("edtAVEITD");
        tb.Enabled = false;
        tb.BackColor = System.Drawing.Color.White;

because apparently the ControlStyle of the templatefield overrules the BackColor of the Textbox, and the textbox remains appearing yellow. The Templatefield can't have an ID, so its properties cannot be adressed... how would I change the background of the Textbox in this case?

1

1 Answers

0
votes

The solution is so easy. just remove taht <ControlStyle BackColor="#e9e015" /> and use inline BackColor="#e9e015" property for your controls. this way you can set the backcolor only for items you need (label and textbox can have different backcolors)

<asp:TemplateField HeaderText="AVEITD:">
  <ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# Bind("AVEITD") %>' BackColor="#e9e015"></asp:Label>
  </ItemTemplate>

  <EditItemTemplate>
    <asp:TextBox ID="edtAVEITD" runat="server" Font-Size="Small" Font-Names="Arial" BackColor="#e9e015"></asp:TextBox>
  </EditItemTemplate>

  <ControlStyle Width="35px" /> <%-- remove: BackColor="#e9e015" --%>

</asp:TemplateField>

then when you set the BackColor in code-behind, it will be applied without problem.

var tb = (TextBox) DetailsView2.FindControl("edtAVEITD");
tb.Enabled = false;
tb.BackColor = System.Drawing.Color.White;