Link to home
Start Free TrialLog in
Avatar of techques
techques

asked on

How to add if condition in a repeater?

Hi

I have a aspx page which uses repeater to list out query result.

There are 2 fileds: type and amount

I want to show that
if type = 1 or 2, add '+' prefix to amount
if type = 3 or 4, add '-' prefix to amount

e.g.

type=2, amount= +344,220.78
type=4, amount= -221,341.30

The following is the code to show those 2 fields.

Here is the psuedo code that I need:

asp:Label ID="amount" runat="server" Text='
if(<%# DataBinder.Eval(Container.DataItem, "type")%> == 1 || <%# DataBinder.Eval(Container.DataItem, "type")%> == 2)
{
"+"
}
if(<%# DataBinder.Eval(Container.DataItem, "type")%> == 3 || <%# DataBinder.Eval(Container.DataItem, "type")%> == 4)
{
"-"
}
<%# string.Format("{0:#,##0.00}",DataBinder.Eval(Container.DataItem, "amount"))%>'></asp:Label>

I use C# and aspx.
<asp:Label ID="type" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "type")%>'></asp:Label>
 
<asp:Label ID="amount" runat="server" Text='<%# string.Format("{0:#,##0.00}",DataBinder.Eval(Container.DataItem, "amount"))%>'></asp:Label>

Open in new window

Avatar of MrAgile
MrAgile
Flag of Australia image

Hi There,

try the code below.
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
 <head>
    <title>OnItemDataBound Example</title>
<script language="C#" runat="server">
       void Page_Load(Object Sender, EventArgs e) {
 
          if (!IsPostBack) {
             ArrayList values = new ArrayList();
 
             values.Add(new Evaluation("Razor Wiper Blades", "Good"));
             values.Add(new Evaluation("Shoe-So-Soft Softening Polish", "Poor"));
             values.Add(new Evaluation("DynaSmile Dental Fixative", "Fair"));
 
             Repeater1.DataSource = values;
             Repeater1.DataBind();
          }
       }
 
       void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
 
          // This event is raised for the header, the footer, separators, and items.
 
          // Execute the following logic for Items and Alternating Items.
          if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
 
             if (((Evaluation)e.Item.DataItem).Rating == "Good") {
                ((Label)e.Item.FindControl("RatingLabel")).Text= "<b>***Good***</b>";
             }
          }
       }    
 
       public class Evaluation {
 
          private string productid;
          private string rating;
 
          public Evaluation(string productid, string rating) {
             this.productid = productid;
             this.rating = rating;
          }
 
          public string ProductID {
             get {
                return productid;
             }
          }
 
          public string Rating {
             get {
                return rating;
             }
          }
       }
 
    </script>
 
 </head>
 <body>
 
    <h3>OnItemDataBound Example</h3>
 
    <form id="form1" runat="server">
 
       <br />
       <asp:Repeater id="Repeater1" OnItemDataBound="R1_ItemDataBound" runat="server">
          <HeaderTemplate>
             <table border="1">
                <tr>
                   <td><b>Product</b></td>
                   <td><b>Consumer Rating</b></td>
                </tr>
          </HeaderTemplate>
 
          <ItemTemplate>
             <tr>
                <td> <asp:Label Text='<%# DataBinder.Eval(Container.DataItem, "ProductID") %>' Runat="server"/> </td>
                <td> <asp:Label id="RatingLabel" Text='<%# DataBinder.Eval(Container.DataItem, "Rating") %>' Runat="server"/> </td>
             </tr>
          </ItemTemplate>
 
          <FooterTemplate>
             </table>
          </FooterTemplate>
 
       </asp:Repeater>
       <br />
 
    </form>
 </body>
 </html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of harwantgrewal
harwantgrewal
Flag of Australia 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
Avatar of techques
techques

ASKER

I did not understant the Mr Agile's code

I tried to use your code:

<%#(((int.Parse(DataBinder.Eval(Container.DataItem, "transactiontypeid")) > 2) ? "+" : "-") + string.Format("{0:#,##0.00}",DataBinder.Eval(Container.DataItem, "amount"))) %>'></asp:Label>

But, it has compile error:
The best overloaded method match for 'int.Parse(string)' has some invalid arguments

It is the sql for the dataset:

select [id]=t.id, [transactiontypeid]=transactiontypeid, [type]=r.type, [clientid]=t.clientid, [nickname]=c.nickname, [currency]=t.currency, [ucurrency]=u.currency, [rate], [amount], [countervalue], [clientreference], [remarks]=t.remarks, [ashortcode]=a.shortcode+ '-' + a.currency + '-' + a.type, [handlingincome], [handlingexpense], [trandatetime], [status] from alltransaction t inner join client c on t.clientid = c.id inner join currency u on t.currency = u.currency inner join transactiontype r on t.transactiontypeid = r.id inner join account a on t.accountid = a.id where alltranid = 0 and t.clientid = '" + id + "' order by [id] asc
I found that it cannot use int.Parse()

It works if I change to ConvertTo.Int32()