Solved

How to add if condition in a repeater?

Posted on 2009-05-09
4
2,814 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now