Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How do I make table data red in <td>?

How do I make table data red in <td>?

Scheduled Pinned Locked Moved C#
questioncsharp
11 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U User 11369001

    There is this code that have a name in the table data and I have been asked to make that data red. Right now it is bold (strong). I need to add code to also make it appear read. How do I do this? Here is the code in C#

    <%# 
        Eval("AttorneyPartyID").ToString() != Eval("PartyID").ToString() ? 
        ""
        : 
        "**Pro Se**" 
    %>
    

    " + Eval("AttorneyFullName") + "

    Richard DeemingR Offline
    Richard DeemingR Offline
    Richard Deeming
    wrote on last edited by
    #2

    Why are you inserting a single-cell table? Just wrap the value in a <span> or a <div>, and set a suitable CSS class on it. You'll also need to HTML encode the value from the database, just in case.

    <ItemTemplate>
    <%#
    Eval("AttorneyPartyID", "{0}") != Eval("PartyID", "{0}")
    ? "<div class=\"attorney-full-name\">" + HttpUtility.HtmlEncode(Eval("AttorneyFullName", "{0}")) + "</div>"
    : "Pro Se"
    %>
    </ItemTemplate>

    .attorney-full-name {
    color: #a00;
    font-weight: bold;
    }


    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

    U 1 Reply Last reply
    0
    • Richard DeemingR Richard Deeming

      Why are you inserting a single-cell table? Just wrap the value in a <span> or a <div>, and set a suitable CSS class on it. You'll also need to HTML encode the value from the database, just in case.

      <ItemTemplate>
      <%#
      Eval("AttorneyPartyID", "{0}") != Eval("PartyID", "{0}")
      ? "<div class=\"attorney-full-name\">" + HttpUtility.HtmlEncode(Eval("AttorneyFullName", "{0}")) + "</div>"
      : "Pro Se"
      %>
      </ItemTemplate>

      .attorney-full-name {
      color: #a00;
      font-weight: bold;
      }


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      U Offline
      U Offline
      User 11369001
      wrote on last edited by
      #3

      It is not a single-cell. I did not paste the whole table (ItemTemplate). The table has 5 rows and each row has a single (cell) Here is the whole table

              <%# 
                          Eval("AttorneyPartyID").ToString() != Eval("PartyID").ToString() ? 
                          ""
                          : 
                          "**Pro Se**" 
                      %>
                      <%# 
                          Eval("AttorneyPartyID").ToString() != Eval("PartyID").ToString() && Eval("AttorneyPhoneNumber") != "" && Eval("AttorneyPhoneNumber") != null ? 
                          ""
                          : 
                          string.Empty 
                      %>
                      <%# 
                          Eval("AttorneyPartyID").ToString() != Eval("PartyID").ToString() && Eval("AttorneyEmail") != "" && Eval("AttorneyEmail") != null ? 
                          ""
                          : 
                          string.Empty 
                      %>
                      <%# 
                          Eval("AttorneyPartyID").ToString() != Eval("PartyID").ToString() ? 
                          "
      

      " + Eval("AttorneyFullName") + "

      Phone:" + ' ' + Eval("AttorneyPhoneNumber") + "

      Email:" + Eval("AttorneyEmail") + "

      "
      :
      string.Empty
      %>

      Richard DeemingR 1 Reply Last reply
      0
      • U User 11369001

        It is not a single-cell. I did not paste the whole table (ItemTemplate). The table has 5 rows and each row has a single (cell) Here is the whole table

                <%# 
                            Eval("AttorneyPartyID").ToString() != Eval("PartyID").ToString() ? 
                            ""
                            : 
                            "**Pro Se**" 
                        %>
                        <%# 
                            Eval("AttorneyPartyID").ToString() != Eval("PartyID").ToString() && Eval("AttorneyPhoneNumber") != "" && Eval("AttorneyPhoneNumber") != null ? 
                            ""
                            : 
                            string.Empty 
                        %>
                        <%# 
                            Eval("AttorneyPartyID").ToString() != Eval("PartyID").ToString() && Eval("AttorneyEmail") != "" && Eval("AttorneyEmail") != null ? 
                            ""
                            : 
                            string.Empty 
                        %>
                        <%# 
                            Eval("AttorneyPartyID").ToString() != Eval("PartyID").ToString() ? 
                            "
        

        " + Eval("AttorneyFullName") + "

        Phone:" + ' ' + Eval("AttorneyPhoneNumber") + "

        Email:" + Eval("AttorneyEmail") + "

        "
        :
        string.Empty
        %>

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #4

        You still need to HTML-encode the database values. And the solution to your original question is still the same: add a CSS class to the parent element of the text you want to style. I also think you can tidy that code up somewhat, and avoid having to build the HTML as a string:

        <asp:TemplateField HeaderText="Attorney" HeaderStyle-HorizontalAlign="Justify" ItemStyle-Width="25%">
        <ItemTemplate>
        <asp:MultiView runat="server" ActiveViewIndex='<%# Eval("AttorneyPartyID", "{0}") != Eval("PartyID", "{0}") ? 0 : 1 %>'>
        <asp:View runat="server">
        <table>
        <tr>
        <td class="attorney-full-name"><%#: Eval("AttorneyFullName") %></td>
        </tr>
        <asp:Placeholder runat="server" visible='<%# !string.IsNullOrEmpty(Eval("AttorneyPhoneNumber", "{0}")) %>'>
        <tr>
        <td class="attorney-phone-number">Phone: <%#: Eval("AttorneyPhoneNumber") %></td>
        </tr>
        </asp:Placeholder>
        <asp:Placeholder runat="server" visible='<%# !string.IsNullOrEmpty(Eval("AttorneyEmail", "{0}")) %>'>
        <tr>
        <td class="attorney-email">Email: <%#: Eval("AttorneyEmail") %></td>
        </tr>
        </asp:Placeholder>
        </table>
        </asp:View>
        <asp:View runat="server">
        Pro Se
        </asp:View>
        </asp:MultiView>
        </ItemTemplate>
        </asp:TemplateField>

        NB: Using <%#: ... %> instead of <%# ... %> - note the extra : at the start - will automatically HTML-encode the output for you. MultiView Class (System.Web.UI.WebControls) | Microsoft Docs[^]


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        U 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          You still need to HTML-encode the database values. And the solution to your original question is still the same: add a CSS class to the parent element of the text you want to style. I also think you can tidy that code up somewhat, and avoid having to build the HTML as a string:

          <asp:TemplateField HeaderText="Attorney" HeaderStyle-HorizontalAlign="Justify" ItemStyle-Width="25%">
          <ItemTemplate>
          <asp:MultiView runat="server" ActiveViewIndex='<%# Eval("AttorneyPartyID", "{0}") != Eval("PartyID", "{0}") ? 0 : 1 %>'>
          <asp:View runat="server">
          <table>
          <tr>
          <td class="attorney-full-name"><%#: Eval("AttorneyFullName") %></td>
          </tr>
          <asp:Placeholder runat="server" visible='<%# !string.IsNullOrEmpty(Eval("AttorneyPhoneNumber", "{0}")) %>'>
          <tr>
          <td class="attorney-phone-number">Phone: <%#: Eval("AttorneyPhoneNumber") %></td>
          </tr>
          </asp:Placeholder>
          <asp:Placeholder runat="server" visible='<%# !string.IsNullOrEmpty(Eval("AttorneyEmail", "{0}")) %>'>
          <tr>
          <td class="attorney-email">Email: <%#: Eval("AttorneyEmail") %></td>
          </tr>
          </asp:Placeholder>
          </table>
          </asp:View>
          <asp:View runat="server">
          Pro Se
          </asp:View>
          </asp:MultiView>
          </ItemTemplate>
          </asp:TemplateField>

          NB: Using <%#: ... %> instead of <%# ... %> - note the extra : at the start - will automatically HTML-encode the output for you. MultiView Class (System.Web.UI.WebControls) | Microsoft Docs[^]


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          U Offline
          U Offline
          User 11369001
          wrote on last edited by
          #5

          Thank for your help on trying to figure this out. I did add css class to existing css file.

          .inactiveAttorney
          {
          color: #AD0000;
          font-weight:bold
          }

          The product owner have asked me to only make the attorney name red when fActive = 0 (false). The code behind have this code

          private bool _fActive;
          public bool fActive
          {
          get { return this._fActive; }
          set { this._fActive = value; }
          }
          this._fActive = parsefActive == true ? true : false; //This is how it is called or populated

          I have successfully been able to use css class to make attorney name red. However, I need help to add a condition so that the name is red only when fActive =false (0). Here is my code which is working and needs adding condition

          "

          " + Eval("AttorneyFullName") + "

          "

          The fActive is a Boolean and else where in the code I see something done when a Boolean is true. I need to do something similar but I do not know how. Here is that example.

          <%# (bool)Eval("PartyConfidentialHomePhone") == true ? "Home":"" %><%#Eval("PartyHomePhone").ToString() != "" && Eval("PartyHomePhone") != null ? "" + Eval("PartyHomePhone") + "":string.Empty%>

          Richard DeemingR 1 Reply Last reply
          0
          • U User 11369001

            Thank for your help on trying to figure this out. I did add css class to existing css file.

            .inactiveAttorney
            {
            color: #AD0000;
            font-weight:bold
            }

            The product owner have asked me to only make the attorney name red when fActive = 0 (false). The code behind have this code

            private bool _fActive;
            public bool fActive
            {
            get { return this._fActive; }
            set { this._fActive = value; }
            }
            this._fActive = parsefActive == true ? true : false; //This is how it is called or populated

            I have successfully been able to use css class to make attorney name red. However, I need help to add a condition so that the name is red only when fActive =false (0). Here is my code which is working and needs adding condition

            "

            " + Eval("AttorneyFullName") + "

            "

            The fActive is a Boolean and else where in the code I see something done when a Boolean is true. I need to do something similar but I do not know how. Here is that example.

            <%# (bool)Eval("PartyConfidentialHomePhone") == true ? "Home":"" %><%#Eval("PartyHomePhone").ToString() != "" && Eval("PartyHomePhone") != null ? "" + Eval("PartyHomePhone") + "":string.Empty%>

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #6

            Try:

            "<table><tr><td class='" + (fActive ? "activeAttorney" : "inactiveAttorney") + "'>" + Eval("AttorneyFullName") + "</td></tr></table>"


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            U 1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              Try:

              "<table><tr><td class='" + (fActive ? "activeAttorney" : "inactiveAttorney") + "'>" + Eval("AttorneyFullName") + "</td></tr></table>"


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              U Offline
              U Offline
              User 11369001
              wrote on last edited by
              #7

              Hi Homer, Thanks for your reply. When I tried the code you gave me, I am getting an error The name "fActive" does not exist in the current context

              Richard DeemingR 1 Reply Last reply
              0
              • U User 11369001

                Hi Homer, Thanks for your reply. When I tried the code you gave me, I am getting an error The name "fActive" does not exist in the current context

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #8

                Then the code-behind of the page or user control where the data-binding is taking place doesn't have the property you showed in your previous message.


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                U 1 Reply Last reply
                0
                • Richard DeemingR Richard Deeming

                  Then the code-behind of the page or user control where the data-binding is taking place doesn't have the property you showed in your previous message.


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  U Offline
                  U Offline
                  User 11369001
                  wrote on last edited by
                  #9

                  Here is the code behind that have the fActive

                  private bool _fActive;
                  public bool fActive
                  {
                  get { return this._fActive; }
                  set { this._fActive = value; }
                  }
                  bool parsefActive = false;Boolean.TryParse(row["fActive"].ToString(), out parsefActive);
                  this._fActive = parsefActive == true ? true : false; //This is how it is called or populated

                  Richard DeemingR 1 Reply Last reply
                  0
                  • U User 11369001

                    Here is the code behind that have the fActive

                    private bool _fActive;
                    public bool fActive
                    {
                    get { return this._fActive; }
                    set { this._fActive = value; }
                    }
                    bool parsefActive = false;Boolean.TryParse(row["fActive"].ToString(), out parsefActive);
                    this._fActive = parsefActive == true ? true : false; //This is how it is called or populated

                    Richard DeemingR Offline
                    Richard DeemingR Offline
                    Richard Deeming
                    wrote on last edited by
                    #10

                    As I said, that doesn't tally with the error message:

                    The name "fActive" does not exist in the current context

                    That property does not exist in the code-behind for the page or user control where your data-binding is taking place.


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                    U 1 Reply Last reply
                    0
                    • Richard DeemingR Richard Deeming

                      As I said, that doesn't tally with the error message:

                      The name "fActive" does not exist in the current context

                      That property does not exist in the code-behind for the page or user control where your data-binding is taking place.


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      U Offline
                      U Offline
                      User 11369001
                      wrote on last edited by
                      #11

                      Hi everybody who tried to help me on this problem. I wanted to thank you for trying to help. However after hours of try and error, I found a solution that works. Just wanted to let you know this is now fixed. Here is my solution. Looks pretty simple! So the logic is, if AttorneyPartyID = PartyID that is Pro Se, else if fActive is false that means the attorney is inactive. Use inactiveAttorney css class to display attorney name with red font else the attorney is active and no need to display name in red font. Display in bold font.

                      <%#
                      Eval("AttorneyPartyID").ToString() == Eval("PartyID").ToString()
                      ? "Pro Se"
                      : (bool) Eval("fActive") == false
                      ? "" /*This is inactive attorney*/
                      :"

                      " + Eval("AttorneyFullName") + "

                      " /*This is Active attorney*/
                      %>

                      " + Eval("AttorneyFullName") + "

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups