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. Web Development
  3. ASP.NET
  4. if statement in the html code in aspx page

if statement in the html code in aspx page

Scheduled Pinned Locked Moved ASP.NET
htmlsysadminhelptutorial
9 Posts 4 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.
  • L Offline
    L Offline
    laziale
    wrote on last edited by
    #1

    Hello, I want to create if statement in the html part of my site. Can someone help me with that and give me some example. I have a label and depending from the text, the forecolor to be different. Here is what I have:

    <asp:Label ID="lblFirstName" runat="server" Text='<%#Server.HtmlEncode(Eval("FirstName").ToString())%>'></asp:Label>

    So depending on the text, for example if the text contains 'John' text will be in red, if the text contains 'Jim' text will be in blue. Thanks for any advice, Laziale

    J S C 4 Replies Last reply
    0
    • L laziale

      Hello, I want to create if statement in the html part of my site. Can someone help me with that and give me some example. I have a label and depending from the text, the forecolor to be different. Here is what I have:

      <asp:Label ID="lblFirstName" runat="server" Text='<%#Server.HtmlEncode(Eval("FirstName").ToString())%>'></asp:Label>

      So depending on the text, for example if the text contains 'John' text will be in red, if the text contains 'Jim' text will be in blue. Thanks for any advice, Laziale

      J Offline
      J Offline
      JHizzle
      wrote on last edited by
      #2

      May I ask why particularly it needs to be in the html code for the page? Are you prevented from altering code behind?

      L 1 Reply Last reply
      0
      • J JHizzle

        May I ask why particularly it needs to be in the html code for the page? Are you prevented from altering code behind?

        L Offline
        L Offline
        laziale
        wrote on last edited by
        #3

        I am trying to solve this issue in the code behind file, but I am trying almost two days now without any success. The thing is that the label is in templatefield within gridview and I am having hard time to solve how I can get to the label in the gridview, check the text and set the forecolor depending on the text. If you have good example how I can solve this in code behind file I will be more then pleased to accept that. Thanks, Laziale

        J 1 Reply Last reply
        0
        • L laziale

          Hello, I want to create if statement in the html part of my site. Can someone help me with that and give me some example. I have a label and depending from the text, the forecolor to be different. Here is what I have:

          <asp:Label ID="lblFirstName" runat="server" Text='<%#Server.HtmlEncode(Eval("FirstName").ToString())%>'></asp:Label>

          So depending on the text, for example if the text contains 'John' text will be in red, if the text contains 'Jim' text will be in blue. Thanks for any advice, Laziale

          S Offline
          S Offline
          Sandeep Mewara
          wrote on last edited by
          #4

          Looks like you are setting the Label text at runtime and server side. Why using a Eval container? Directly in the server side, use a switch statment, based on various case, set the back/fore color of the label and also set the text!

          L 1 Reply Last reply
          0
          • S Sandeep Mewara

            Looks like you are setting the Label text at runtime and server side. Why using a Eval container? Directly in the server side, use a switch statment, based on various case, set the back/fore color of the label and also set the text!

            L Offline
            L Offline
            laziale
            wrote on last edited by
            #5

            I am trying this on button event:

            for (int i = 0; i < gvPpl.Rows.Count; i++)
            {
            Label lblL = (Label)gvPpl.Rows[i].Cells[2].FindControl("lblFirstName");
            }

            but nothing comes up. Can you give me some example how I can do that, and how I can set the forecolor afterwards. Eval container was there before I started working on this code. Please advice if I can/need to use something else and still get the same data. Thx, Laziale

            1 Reply Last reply
            0
            • L laziale

              I am trying to solve this issue in the code behind file, but I am trying almost two days now without any success. The thing is that the label is in templatefield within gridview and I am having hard time to solve how I can get to the label in the gridview, check the text and set the forecolor depending on the text. If you have good example how I can solve this in code behind file I will be more then pleased to accept that. Thanks, Laziale

              J Offline
              J Offline
              JHizzle
              wrote on last edited by
              #6

              Ahhh ok. So, from my experience, you're probably going to want to hook into the databind event on a row, find your label control using fincontrol on the row being passed and then change the colour there. Example:

              protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
              {

              Label myLabel = (Label)e.Row.FindControl("yourlabel");

              if (myLabel.Text == "Jack")
              {
              'change the colour here
              }

              }

              There might be an easier way but that's just off the top of my head.

              1 Reply Last reply
              0
              • L laziale

                Hello, I want to create if statement in the html part of my site. Can someone help me with that and give me some example. I have a label and depending from the text, the forecolor to be different. Here is what I have:

                <asp:Label ID="lblFirstName" runat="server" Text='<%#Server.HtmlEncode(Eval("FirstName").ToString())%>'></asp:Label>

                So depending on the text, for example if the text contains 'John' text will be in red, if the text contains 'Jim' text will be in blue. Thanks for any advice, Laziale

                S Offline
                S Offline
                Sandeep Mewara
                wrote on last edited by
                #7

                Based on what now you say, that this is not an independant label and is in the GridView template field... Here you go:

                void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
                {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                Label lblFirstName= (Label)e.Item.FindControl("lblFirstName");
                if(lblFirstName.Text == "John")
                lblFirstName.ForeColor=Color.Red;
                else if(lblFirstName.Text == "Jim")
                lblFirstName.ForeColor= Color.Blue;
                }
                }

                Lastly, i would suggest to pick a book and read before mocing ahead with development. This is because 2-3 threads below you had been given almost 80% of the code and yet you are unable to get it.

                L 1 Reply Last reply
                0
                • S Sandeep Mewara

                  Based on what now you say, that this is not an independant label and is in the GridView template field... Here you go:

                  void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
                  {
                  if (e.Row.RowType == DataControlRowType.DataRow)
                  {
                  Label lblFirstName= (Label)e.Item.FindControl("lblFirstName");
                  if(lblFirstName.Text == "John")
                  lblFirstName.ForeColor=Color.Red;
                  else if(lblFirstName.Text == "Jim")
                  lblFirstName.ForeColor= Color.Blue;
                  }
                  }

                  Lastly, i would suggest to pick a book and read before mocing ahead with development. This is because 2-3 threads below you had been given almost 80% of the code and yet you are unable to get it.

                  L Offline
                  L Offline
                  laziale
                  wrote on last edited by
                  #8

                  First, thanks for your reply. I tried that thing too, but I am getting no value for the label. Here is what I have: in the html part:

                  <div style="padding-left: 10px; padding-right: 10px; text-align: left; width:45%">
                  <asp:Label ID="lblFirstName" runat="server" Text='<%#Server.HtmlEncode(Eval("FirstName").ToString())%>'></asp:Label>
                  </div>

                  in the code behind:

                  protected void gvPpl_DataBound(object sender, GridViewRowEventArgs e)
                  {

                          for (int i = 0; i < gvPpl.Rows.Count; i++)
                          {     
                              Label lblL = (Label)gvPpl.Rows\[i\].Cells\[2\].FindControl("lblFirstName");
                              Label lblL1 = (Label)gvPpl.Rows\[i\].Cells\[1\].FindControl("lblFirstName");
                              Label lblL2 = (Label)gvPpl.Rows\[i\].Cells\[0\].FindControl("lblFirstName");
                              Label lblL3 = (Label)gvPpl.Rows\[i\].Cells\[3\].FindControl("lblFirstName");
                          }
                      }
                  

                  I am trying with different cells, just in case I have the wrong cell, but that is not helping, all the labels have no text. Any advice? Thx, Laziale

                  1 Reply Last reply
                  0
                  • L laziale

                    Hello, I want to create if statement in the html part of my site. Can someone help me with that and give me some example. I have a label and depending from the text, the forecolor to be different. Here is what I have:

                    <asp:Label ID="lblFirstName" runat="server" Text='<%#Server.HtmlEncode(Eval("FirstName").ToString())%>'></asp:Label>

                    So depending on the text, for example if the text contains 'John' text will be in red, if the text contains 'Jim' text will be in blue. Thanks for any advice, Laziale

                    C Offline
                    C Offline
                    carlecomm
                    wrote on last edited by
                    #9

                    Hi, If you just want to change the forecolor, you can try this: string test1 = "John"; string test2 = "Jim"; string testStr = Label1.Text;//source string if (testStr.IndexOf(test1) > 0) Label1.ForeColor = System.Drawing.Color.Red; else if (testStr.IndexOf(test2) > 0) Label1.ForeColor = System.Drawing.Color.Blue; Hope it helps:)

                    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