-1

I recently started working with some legacy ASP.NET stuff, and I've run into an interesting problem:

I've got a table displaying a few values, some of which are evaluated in C# server-side. They all work correctly. Then all of a sudden...

<td><asp:Label ID="Label2" runat="server" class='<%=SevenDayThresholdTooHighOrLow%>'><%=ChgFromSevenDaysAgoInfo%></asp:Label></td>

ChgFromSevenDaysAgoInfo is evaluated properly. SevenDayThresholdTooHighOrLow is rendered as a string inside of the class quotations. That is class="<%=SevenDayThresholdTooHighOrLow%>".

In the code-behind file, the two variables are declared in the same scope, and assigned values pretty much one after the other. My Visual Studio doesn't complain about not finding certain variables in code like it would if the property did not exist.

What other factors could be influencing this oddity? What could I have missed?

Thank you very much for your help, everyone!

  • Eli

EDIT: I took a look at what is the use of Eval() in asp.net and tried to set my tag up that way (class='<%# Eval("SevenDayThresholdTooHighOrLow")%>'), unfortunately to no effect.

Community
  • 1
  • 1
justian17
  • 575
  • 1
  • 7
  • 17

2 Answers2

1

That class attribute probably isn't being evaluated for server-side code, likely because class isn't a property of Label. (Label isn't an HTML element, it's a server-side control. So instead of HTML attributes it uses object properties.)

There's a CssClass property, you might try that instead. But even then the approach is different because the system may still not attempt to evaluate server-side code injected into already server-side components. Still, worth a try.

What should definitely work is setting the CssClass property in the code-behind:

Label2.CssClass = SevenDayThresholdTooHighOrLow;

If you want to keep this out of code-behind (and who could blame you?) then another approach could be to replace the server-side control with an HTML element entirely. The idea being that if properties on this control aren't otherwise being set in server-side code, then does it really need to be a server-side control? If I remember correctly, a Label emits as a span:

<span class="<%=SevenDayThresholdTooHighOrLow%>"><%=ChgFromSevenDaysAgoInfo%></span>
David
  • 208,112
  • 36
  • 198
  • 279
1

You can't do that in just the markup code. You can't use server tags in a server control. You could use a data binding tag, but then you would need to trigger the data binding from the code behind anyway.

Just set the attribute from the code behind:

Label2.CssClass = SevenDayThresholdTooHighOrLow;
Guffa
  • 687,336
  • 108
  • 737
  • 1,005