Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Having a repeater item collection problem...

  • 06-03-2013 11:22am
    #1
    Closed Accounts Posts: 83 ✭✭


    Hi Folks,

    I've a problem that I can't get my head around. I have a repeater that returns a collection of items based on a 3 tiered dropdownlist solution. This works fine, basically it is a product sorting solution that returns a list of stock based on what is selected from the 3 dropdown lists. The repeater in this respect, works fine.

    Each item in the collection returns product info, price, description, stockcode, and QUANTITY.

    Based on customer feedback, I decided to try to implement a bit of functionality that uses one of three small gif images, (traffic light system, red, orange or green), to show the stock level status for any product that is returned via my repeater. (Green for more than 1 in stock, orange for only one left in stock, and red for 0 stock.

    I've implemented code for this which is working fine, except for the fact that it in 100% of cases, will ignore the first item in the list that is returned. My traffic light type solution is working perfectly for every other item in the list except for the first item, no matter what variation of products are returned by changing the selection on the dropdownlists.

    I can't for the life of me work out why this is happening as the code is really quite simple from where I'm standing and this shouldn't be happening.

    Any suggestions HUGELY appreciated!

    aspx code:
        <asp:Repeater ID="_Catalog" runat="server" OnItemDataBound="_Catalog_ItemDataBound">
    
        <ItemTemplate>
        <table border="0" width="350px" style="border-collapse:collapse; border-width:1px; font-size:1em; border-color:#111; font-size:1em; border-style:dashed; background-color:Silver;">
    
    
        <tr>
        <td valign="top" align="right"><b>Make: </b></td><td><b><%# ((Product)Container.DataItem).Name %></b></td>
        </tr>
        
        <tr><td></td></tr>
        
        <tr>
        <td valign="top" align="right"><b>Image: </b></td><td align="justify" style="font-family: Verdana; font-weight: bold; font-style: normal; color: #FF3300">
        <link rel="stylesheet" href="css/lightbox.css" type="text/css" media="screen" />
        <a href="ViewClutchKitProductImage.aspx?img=<%# ((Product)Container.DataItem).Id %>" rel="lightbox" style="color: #FF3300; font-family: Verdana; font-size:inherit; font-weight: normal;">
        <%# ((Product)Container.DataItem).Description %></a></td>
         </tr>
        
        <tr><td></td></tr>
        
        <tr>
        <td valign="top" align="right"><b>Price: </b></td><td>&#8364;<%# ((Product)Container.DataItem).InvoiceAmountDue.ToString("F2") %></td>
        </tr>
        
        <tr>
        <td valign="top" align="right"><b>Code: </b></td><td><%# ((Product)Container.DataItem).StockCode.ToString() %></td>
        </tr>
            
        <tr>
        <td valign="top" align="right"><b>In Stock: </b></td><td valign="top"><asp:Image ID="InStock" ImageAlign="Top" runat="server" /><asp:Label ID="BracketLeft" runat="server" Text=" ("></asp:Label><asp:Label ID="QuantityDisplay" runat="Server" Text='<%# ((Product)Container.DataItem).Quantity.ToString() %>'></asp:Label><asp:Label ID="Label1" runat="server" Text=")"></asp:Label></td>
        </tr>
        
        <tr><td></td></tr>
        
       
        
        <tr><td></td></tr>
        
        <tr>
        <td></td><td><asp:Button ID="_AddToCart" CommandArgument="<%# ((Product)Container.DataItem).Id.ToString() %>" CommandName="AddToCart" runat="server" Text="Buy This Product" Font-Bold="True" Font-Names="verdana" Font-Size="Smaller" ForeColor="#FF3300" /><br /><br /> </td>
        </tr>
        </table><br /><br />
        </ItemTemplate>
        
        </asp:Repeater>
    

    Codebehind:
    protected void _Catalog_ItemDataBound(object source, RepeaterItemEventArgs e)
            {
                Label QuantityDisplayLabel = (Label)e.Item.FindControl("QuantityDisplay") as Label;
                int _Quan = Int32.Parse(QuantityDisplayLabel.Text);
    
                foreach (RepeaterItem item in _Catalog.Items)
                {
    
    
                    if (_Quan > 1)
                    {
                        Image img1 = (Image)e.Item.FindControl("InStock");
                        if (img1 != null)
                        {
                            img1.ImageUrl = "~/Images/InStock.gif";
                        }
                    }
    
                    if (_Quan == 1)
                    {
                        Image img2 = (Image)e.Item.FindControl("InStock");
                        if (img2 != null)
                        {
                            img2.ImageUrl = "~/Images/LowStock.gif";
                        }
                    }
    
                    if (_Quan == 0)
                    {
                        Image img3 = (Image)e.Item.FindControl("InStock");
                        if (img3 != null)
                        {
                            img3.ImageUrl = "~/Images/NoStock.gif";
                        }
                    }
    
    
                }
            }
    


Comments

  • Registered Users, Registered Users 2 Posts: 1,311 ✭✭✭Procasinator


    I don't if it's related to your problem, but while are you looping over _Catalog.Items?

    Your method should look something like this:
    protected void _Catalog_ItemDataBound(object source, RepeaterItemEventArgs e)
    {
    	if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    	{
    		Image trafficLight = (Image)e.Item.FindControl("InStock");
    		Product product = (Product)e.Item.DataItem;
    		if (product.Quantity > 1)
    		{
    			trafficLight.ImageUrl = "~/Images/InStock.gif";
    		}
    		else if (product.Quantity == 1)
    		{
    			trafficLight.ImageUrl = "~/Images/LowStock.gif";
    		}
    		else
    		{
    			img1.ImageUrl = "~/Images/NoStock.gif";
    		}
    	}
    }
    


  • Closed Accounts Posts: 83 ✭✭LordSinclair


    Thanks a mil for that Procrastinator, that's sorted out the issue, I thought though I'd have to iterate through the collection of items returned?!?


  • Registered Users, Registered Users 2 Posts: 1,311 ✭✭✭Procasinator


    Thanks a mil for that Procrastinator, that's sorted out the issue, I thought though I'd have to iterate through the collection of items returned?!?

    ItemDataBound is called for each row after it is bound (including headers and footers). You want to only look at the data for the current row, not all rows.

    If you look at your code, you will see that you looped through _Catalog.Items and assigned each item to a variable called item, yet you never use this variable within your foreach loop.

    What you were effectively doing is setting the image N amount of times, N being the value of _Catalog.Items.Count at that particular call.

    I'm unsure, but I would guess that _Catalog.Items.Count is 0 the first time the code is run, and hence, the foreach contents are never executed. It's probably after an item is used in ItemDataBound that it is added to the _Catalog.Items collection.


Advertisement