if statement in the html code in aspx page
-
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
-
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
-
May I ask why particularly it needs to be in the html code for the page? Are you prevented from altering code behind?
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
-
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
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 variouscase
, set the back/fore color of the label and also set the text! -
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 variouscase
, set the back/fore color of the label and also set the text!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
-
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
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.
-
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
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.
-
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.
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
-
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
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:)