-1

I am trying to dynamically change the values inside my HTML markup depending on certain conditions in the code-behind.

In my code-behind in the Page_Load(), I have the following:

If Not myCheck = 1 Then
    mySuffix = "_gr"
    myColor = "#cccccc"
Else
    mySuffix = ""
    myColor = "#bdd1ec"
End If

This is used for my HTML markup to change the way the page looks on certain conditions.

<table>
   ...
   <tr>
      <td style="background-color: <%=myColor%>" colSpan="6"><IMG height="5" src="/images/corner-inner-blue-topLeft<%=mySuffix%>.gif" width="5"></td>
      <td style="background-color: <%=myColor%>; text-align:right"><IMG height="5" src="/images/corner-inner-blue-topRight<%=mySuffix%>.gif" width="5"></td>
   </tr>
   ...
</table>

Now my problem is this. It works on the suffix but not on the color. When I run my site, navigate to the page, and I do an inspect element, I get this.

<table>
   ...
   <tr>
      <td style="background-color: <%=myColor%>" colspan="6"><img src="/images/myImg_gr.gif" height="5" width="5"></td>
      <td style="background-color: <%=myColor%>; text-align:right"><img src="/images/myImg_gr.gif" height="5" width="5"></td>
   </tr>
   ...
</table>

It's reading the <%=mySuffix%> variable from my code-behind but not the <%=myColor%>. I tried debugging just to be sure that it does pass something to the variable and it does.

Can someone help me point out which I am doing wrong? Or is there any other better way to achieve this?

Thanks a lot!

Smiley
  • 3,207
  • 13
  • 49
  • 66

1 Answers1

0

If not using the table is an option, you might consider using a span or div element, and give it an id, instead of using a <td> element. Then you can reference the span control inside the code-behind to set the style properties. Like this:

ASPX:

<span id="spnSample" runat="server">Hello</span>

.VB:

If Not myCheck = 1 Then
        mySuffix = "_gr"
        spnSample.Style.Add("background-color", "#cccccc")
    Else
        mySuffix = ""
        spnSample.Style.Add("background-color", "#bdd1ec")
    End If
killQuotes
  • 178
  • 13