Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to add if condition in a repeater?

Posted on 2009-05-09
4
Medium Priority
?
2,823 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 150 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Loops Section Overview
Suggested Courses

972 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