Solved

How to add if condition in a repeater?

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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.…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

19 Experts available now in Live!

Get 1:1 Help Now