?
Solved

How to write if condition in repeater?

Posted on 2009-02-17
19
Medium Priority
?
1,154 Views
Last Modified: 2013-12-17
Hi

I have the following code need to add a if condition

if (<%# DataBinder.Eval(Container.DataItem, "expire") %> == 1)
then
display image
Existing code without checking expire==1
<a href="product.aspx?id=<%# DataBinder.Eval(Container.DataItem, "productid") %>"><img src="images/toy.gif" /></a>
 
How to add a condition?
 
if (<%# DataBinder.Eval(Container.DataItem, "expire") %> == 1)
then 
display
 
<a href="product.aspx?id=<%# DataBinder.Eval(Container.DataItem, "productid") %>"><img src="images/toy.gif" /></a>

Open in new window

0
Comment
Question by:techques
  • 10
  • 9
19 Comments
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23666458
How about just re-writing your data query.

SELECT * FROM Products WHERE expire != 1
0
 

Author Comment

by:techques
ID: 23666553
no, as there is a fill to show that product status was expired.

however, the expired product will not show that image linking to product page as it was expired
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23666583
Ok, you could use an asp:HyperLink control in your repeater, and then intercept it in the Repeater_ItemDataBound

HyperLink lnk = e.Item.FindControl("Link1");
if(e.Item.DataItem("expired") == 1)
            // do something


0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 

Author Comment

by:techques
ID: 23666631
if I use

<asp:HyperLink ID="lnk" runat="server"><img src="images/toy.gif" /></asp:HyperLink>

the image still displays, right?

but it needs the image not display.

And, I do not understand what is 'e' in e.Item.FindControl("Link1");

Could you show me more code?
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23666670
Sure.
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="ItemDataBound">
</asp:Repeater>
 
protected void ItemDataBound(object source, RepeaterCommandEventArgs e)
  {
    HyperLink lnk = (HyperLink)e.Item.FindControl("lnk");            
  }

Open in new window

0
 

Author Comment

by:techques
ID: 23666744
Hi

It has compile error in e.Item.DataItem("expire") :

'System.Web.UI.WebControls.RepeaterItem.DataItem' is attribute, but treat it as method to use.
In aspx, 
 
<asp:HyperLink ID="lnk" runat="server"><img src="images/toy.gif"  /></asp:HyperLink>
 
In cs,
 
protected void ItemDataBound(object source, RepeaterCommandEventArgs e)
        {
            HyperLink lnk = (HyperLink)e.Item.FindControl("lnk");
            if (e.Item.DataItem("expire") == 1)
            {                
                lnk.NavigateUrl = "product.aspx?id=" + e.Item.DataItem("productid");
                //and display image
            }
           else
            {
                 //no link and no image
            }
        }

Open in new window

0
 
LVL 15

Accepted Solution

by:
aibusinesssolutions earned 150 total points
ID: 23666890
Sorry, that's how you access the DataItem in vb.net, it's a little trickier in C#.
if(e.Item.ItemType == ListItemType.Item)
            {
                HyperLink lnk = (HyperLink)e.Item.FindControl("lnk");
                System.Data.DataRowView dv = (System.Data.DataRowView)e.Item.DataItem;
                if (dv.Row["status"].ToString() == "1")
                {
                    //do something
                }
                else
                {
                    //do something else
                }
            }

Open in new window

0
 

Author Comment

by:techques
ID: 23666929
It still has compile error:

No overload for 'ItemDataBound' matches delegate 'System.Web.UI.WebControls.RepeaterItemEventHandler'

I changed the code:
<ASP:REPEATER id="repeaterItems" runat="server" OnItemDataBound="ItemDataBound">
 
protected void ItemDataBound(object source, RepeaterCommandEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item)
            {
                HyperLink lnk = (HyperLink)e.Item.FindControl("lnk");
                System.Data.DataRowView dv = (System.Data.DataRowView)e.Item.DataItem;
                if (dv.Row["expire"].ToString() == "1")
                {
                    lnk.NavigateUrl = "product.aspx?id=" + dv.Row["productid"].ToString();
                }
            }
        }

Open in new window

0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23666947
Oh, wrong definition, VB has me so spoiled.

It should be RepeaterItemEventArgs

(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
0
 

Author Comment

by:techques
ID: 23667086
It does not has compile error.

However there are 2 problems:
1) There are 10 rows of data, only the 10th one has expired.
However, the hyperlink only be available for 1,3,5,7,9
2,4,6,8 all disabled.

2) For the item 10, the image still displays although it has expired.
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23667135
I would either use an asp:Image tag, or use the ImageUrl of the HyperLink tag.

<asp:HyperLink runat="server" ID="lnk" ImageUrl="image/toy.gif"></asp:HyperLink>

if (dv.Row["expire"].ToString() == "1")
   {
      lnk.ImageUrl = "";
      lnk.NavigateUrl = "product.aspx?id=" + dv.Row["productid"].ToString();
   }

Or

<asp:HyperLink runat="server" ID="lnk" ImageUrl=""><asp:Image runat="server" ID="image1" /></asp:HyperLink>

if (dv.Row["expire"].ToString() == "1")
   {
      Image img = (Image)e.FindControl("image1");
      img.Visible = False;
      lnk.NavigateUrl = "product.aspx?id=" + dv.Row["productid"].ToString();
   }
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23667141
That should be e.Item.FindControl("image1")
0
 

Author Comment

by:techques
ID: 23667189
I tried both methods still not work.

For the first method, it was the same as before. display all images in all rows and only 1,3,5,7,9 have hyperlink.

For the 2nd method, it has compile errors:
                    Image img = (Image)e.FindControl("image1");
                    img.Visible = False;

2 compile errors:
'System.Web.UI.WebControls.RepeaterItemEventArgs'  not include 'FindControl'  definition
'False'  not exist in current content

      
Also, the code did not mention the path of it if it is visible.
<img src="images/toy.gif"  />
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23667199
Yes, for the 2nd method, it should be e.Item.FindControl, not e.FindControl
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23667200
Make sure you change the hyperlink code


<asp:HyperLink runat="server" ID="lnk">
<asp:Image runat="server" ID="image1" />
</asp:HyperLink>

Open in new window

0
 

Author Comment

by:techques
ID: 23667357
Hi

I changed to
 Image img = (Image)e.Item.FindControl("image1");
 img.Visible = true;

1) It shows a broken image as the code did not tell the path of the image.
2) Still the same, only 1,3,5,7,9 have hyperlink
3) 10th image still display even its expire == 0

expire 1 = display
expire 0 = hidden
0
 

Author Comment

by:techques
ID: 23667533
I changed the code to be:

but still only 1,3,5,7,9 display images.

2,4,6,8,10 images are not available

and the 10th image still display.

I am not sure either the repeater OnItemDataBound or if (dv.Row["expire"].ToString() == "1") has problem.
Image img = (Image)e.Item.FindControl("image1");
                img.ImageUrl = "images/toy.gif";
                if (dv.Row["expire"].ToString() == "1")
                {
                    img.Visible = true;
                    lnk.NavigateUrl = "product.aspx?id=" + dv.Row["productid"].ToString();
                }
                else
                {
                    img.Visible = false;
                }

Open in new window

0
 

Author Comment

by:techques
ID: 23668065
I asked other experts and got the solution at http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_24153063.html

thanks for help
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23670167
Oh yeah, I forgot about the alternating items, sorry about that, it was pretty late last night when I was trying to help. Glad you got it working.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

850 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