0

I have a datalist in my website and each data item has a label with backcolor. The datasource is set and I have a label with backcolor which is set from a color Code in datasource :

<asp:DataList runat="server" ID="RptColor" DataSourceID="DSColor" RepeatDirection="Horizontal" RepeatLayout="Table">
    <ItemTemplate>
        <div class="col-md-1">
            <asp:Label runat="server" ID="RpLblColorCode" BackColor='<%# System.Drawing.Color.FromName(Eval("ColorCode").ToString()) %>' Width="20px" Height="20px"></asp:Label>
        </div>
    </ItemTemplate>
</asp:DataList>

the problem is that when I press a button and postback occurs the back color would be gone! ... I have an updatepanel on the page. please help me

VDWWD
  • 35,079
  • 22
  • 62
  • 79
mari Mir
  • 39
  • 1
  • 7
  • Please write your code behind. – Farzin Kanzi Apr 08 '17 at 10:36
  • I don't write anything in code behind .. I just have this code for datasource in html : – mari Mir Apr 08 '17 at 10:39
  • yes it is all in an update panel – mari Mir Apr 08 '17 at 10:51
  • It is hex code.. I've tried your code and I get this error ==> Specified cast is not valid – mari Mir Apr 08 '17 at 11:05
  • Do you know how to **Inspect Element** ? If yes after post back inspect your label and see the bg property value. Do you have another databound in update panel? Do they work truly? – Farzin Kanzi Apr 08 '17 at 11:14
  • I don't know how to inspect element and I have another databound in the updatepanel and it works correctly ... all of the other columns in the daatabound with backcolor are showing correctly .. the problem is just the backcolor – mari Mir Apr 09 '17 at 06:53

1 Answers1

0

Use ColorTranslator.FromHtml instead of Color.FromName. You can change your code to this:

<asp:DataList runat="server" ID="RptColor" DataSourceID="DSColor" RepeatDirection="Horizontal" RepeatLayout="Table">
  <ItemTemplate>
     <asp:Label runat="server"
      ID="RpLblColorCode" BackColor='<%# System.Drawing.ColorTranslator.FromHtml(Eval("ColorCode").ToString()) %>'></asp:Label>
  </ItemTemplate>
</asp:DataList>
Farzin Kanzi
  • 3,380
  • 2
  • 21
  • 23