Solved

How to add if condition in a repeater?

Posted on 2009-05-09
4
2,815 Views
Last Modified: 2013-12-17
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

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

Expert Comment

by:MrAgile
ID: 24346990
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

0
 
LVL 5

Accepted Solution

by:
harwantgrewal earned 50 total points
ID: 24350857
Just wrote quick sample for you. You can also do the way Mr Agile have explained.


<%@ 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 runat="server">
    <title>EESolution</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:XmlDataSource DataFile="TestData.xml" XPath="RootElement/testData" ID="xmlDataSource"
        runat="server"></asp:XmlDataSource>
    <div>
        <table cellpadding="0" width="200" cellspacing="0" border="0">
            <asp:Repeater DataSourceID="xmlDataSource" ID="EERepeater" runat="server">
                <HeaderTemplate>
                    <tr>
                        <td>
                            Type
                        </td>
                        <td align="right">
                            Result
                        </td>
                    </tr>
                </HeaderTemplate>
                <SeparatorTemplate>
                    <tr>
                        <td colspan="2" style="border: 1px solid black; background-color: #000000;height:3px;">
                        </td>
                    </tr>
                </SeparatorTemplate>
                <ItemTemplate>
                    <tr>
                        <td>
                            <%#XPath("type")%>
                        </td>
                        <td align="right">
                            <%#(((int.Parse(XPath("type").ToString()) > 2) ? "+" : "-") + XPath("number"))%>
                            
                        </td>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>
        </table>
    </div>
    </form>
</body>
</html>
 
 
 
 
<?xml version="1.0" encoding="utf-8" ?>
<RootElement>
  <testData>
    <type>1</type>
    <number>1234871</number>
  </testData>
  <testData>
    <type>2</type>
    <number>2348</number>
  </testData>
  <testData>
    <type>2</type>
    <number>3871</number>
  </testData>
  <testData>
    <type>4</type>
    <number>231171</number>
  </testData>
  <testData>
    <type>4</type>
    <number>26571</number>
  </testData>
  <testData>
    <type>3</type>
    <number>234889</number>
  </testData>
</RootElement>

Open in new window

0
 

Author Comment

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

Author Comment

by:techques
ID: 24361290
I found that it cannot use int.Parse()

It works if I change to ConvertTo.Int32()
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

831 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