Link to home
Start Free TrialLog in
Avatar of techques
techques

asked on

How to add if(){}else{} to repeater databind in ASPX?

Hi
I use repeater to get and present the data. It shows fine. But, I want to display another text with certain condition, how can I do that?

Here is the code:
<%# Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(Convert.ToDecimal(DataBinder.Eval(Container.DataItem, "price")) * 2))).ToString() %>
 
How to change it to:
<%# 
if( Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(Convert.ToDecimal(DataBinder.Eval(Container.DataItem, "bids")) * 2))) < 50) 
{
print("Less than 50");
}
else
{
print("Larger than 50");
}
%>

Open in new window

Avatar of lotusnotesnewbie
lotusnotesnewbie
Flag of India image

Try
<%# (Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(Convert.ToDecimal(DataBinder.Eval(Container.DataItem, "bids")) * 2)))<50)? "Less than 50":"Larger than 50" %>

That is if you have a label control, the code will look like
<asp:Label CssClass="tds" ID="Label1" runat="server" Text='<%# (Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(Convert.ToDecimal(DataBinder.Eval(Container.DataItem, "bids")) * 2)))<50)? "Less than 50":"Larger than 50" %>'></asp:Label>
simply make it as more standard wise like

<asp:Label CssClass="tds" ID="Label1" runat="server" Text='
CheckIfLarger(
Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(Convert.ToDecimal(DataBinder.Eval(Container.DataItem, "price")) * 2))).ToString()
)
'></asp:Label>

then on the cs page write a function like

public string CheckIfLarger(string str)
{
 if(Int.Parse(str)>50)
  return("Larger than 50");
 else
 return("Less than 50");
}



Avatar of techques
techques

ASKER

I do not have label control, if the condition changes to
<=50 ? "Less than 50"
>50 && <=100 ? "Larger than 50 but smaller than 100"
>100 ? "Larger than 100"

How should the following code be changed?
<%# (Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(Convert.ToDecimal(DataBinder.Eval(Container.DataItem, "bids")) * 2)))<50)? "Less than 50":"Larger than 50" %>

Open in new window

<%#
CheckIfLarger(
Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(Convert.ToDecimal(DataBinder.Eval(Container.DataItem, "price")) * 2))).ToString()
)
%>

then on the cs page write a function like



public string CheckIfLarger(string str)
{
if(Int.Parse(str)>50)
 return("Larger than 50");
else
return("Less than 50");
} 

Open in new window

But, it has 3 conditions:
<=50 ? "Less than 50"

>50 && <=100 ? "Larger than 50 but smaller than 100"

>100 ? "Larger than 100"

just change the function

public string CheckIfLarger(string str)
{
if(Int.Parse(str)>100)
 return("Larger than 100");
else if(Int.Parse(str)>50)
 return("Larger than 50");
else
return("Less than 50");
} 

Open in new window

Int.Parse cannot be compiled in C#
I changed to convert.toint32, but it has run time error
ASKER CERTIFIED SOLUTION
Avatar of Praveen Venu
Praveen Venu
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial