?
Solved

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

Posted on 2009-02-09
8
Medium Priority
?
800 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:techques
  • 4
  • 3
8 Comments
 
LVL 4

Expert Comment

by:lotusnotesnewbie
ID: 23598315
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>
0
 
LVL 15

Expert Comment

by:Praveen Venu
ID: 23598616
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");
}



0
 

Author Comment

by:techques
ID: 23600262
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

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 15

Expert Comment

by:Praveen Venu
ID: 23600292
<%#
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

0
 

Author Comment

by:techques
ID: 23600581
But, it has 3 conditions:
<=50 ? "Less than 50"

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

>100 ? "Larger than 100"

0
 
LVL 15

Expert Comment

by:Praveen Venu
ID: 23600593
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

0
 

Author Comment

by:techques
ID: 23600763
Int.Parse cannot be compiled in C#
I changed to convert.toint32, but it has run time error
0
 
LVL 15

Accepted Solution

by:
Praveen Venu earned 150 total points
ID: 23600812
try this

    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");
}



if error post the details
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Loops Section Overview
Suggested Courses
Course of the Month13 days, 13 hours left to enroll

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question