Click here to Skip to main content
14,920,592 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
I have a repeater that it has image inside itself. I want to show different image base on ranking for example if ranking is 1 I show an image and if it is 2 I show the other etc.and i have 5 kind of image and 5 grade too.

Ranking is a column in dataset. But my function does not work correctly and I can't get correct result. It only shows first pic. What solution do you suggest for this operation??

thank a lot.

This is my code
C#
public string getimg()
{ 
    SqlConnection con = new SqlConnection("data source=.;database=site;integrated security=true;");
     string sSQL = "Select username ,weight,point , Rank() over(order by point desc) as 'ranking' from karbar order by point desc";  
     SqlCommand cmd = new SqlCommand(sSQL, con);
     SqlDataAdapter adapt = new SqlDataAdapter(cmd);
     DataSet ds = new DataSet();
     adapt.Fill(ds);
     foreach (DataRow myRow in ds.Tables[0].Rows)
     {
         if (Convert.ToInt32(myRow["ranking"]) == 1)

         { return "price/con1.png"; }
         else return "price/con2.png";
     }

     }

and its html
ASP.NET
<asp:Repeater ID="rptList" runat="server" OnItemDataBound="rptList_ItemDataBound">  
<HeaderTemplate>  
<table>  
<tr>  

<th>  

</th>  
<th>  
<asp:Label ID="Label14" Text="point" runat="server"></asp:Label>  
</th>  
<th>  
<asp:Label ID="Label1" Text="whight" runat="server"></asp:Label>  
</th>  
<th>  
<asp:Label ID="Label2" Text="average" runat="server"></asp:Label>  
</th>  
<th>  
<asp:Label ID="Label3" Text="ranking" runat="server"></asp:Label>  
</th>  
<th>  
<asp:Label ID="Label6" Text="name" runat="server"></asp:Label>  
</th>  
</tr>  
</HeaderTemplate>  
<ItemTemplate> 

        <div class="outer">
            <div class="inner">
               
               
                 <div class="innerContent" style=" width: 53px;  text-align:center">
                    <%#DataBinder.Eval(Container.DataItem,"point") %>
                </div>
                 <div class="innerContent"  style=" width: 53px; text-align:center" ">
                    <%#DataBinder.Eval(Container.DataItem,"weight") %>
                </div>
                <div class="innerContent" style=" width: 53px; text-align:center">
                    <%#DataBinder.Eval(Container.DataItem,"average") %>
                </div>
                 <div class="innerContent" style=" width: 140px; text-align:center">
                    <asp:Label ID="Label7" runat="server" Text='<%# Eval("username") %>' Width="138"></asp:Label>
                    
                </div>
                <div class="innerContent" style=" width: 53px; text-align:center">
                    <%#DataBinder.Eval(Container.DataItem, "ranking")%>
                </div>
                 <div class="innerTitle">
                    <img style="width:53px;height:52px" alt=""  src="<%# getimg() %>" /></div>
                     
            </div>
            <div class="clear">
            </div>
            
       
        </div>                                
        </div>
    </ItemTemplate>

<FooterTemplate>  
</table>  
</FooterTemplate>  
</asp:Repeater>
Posted
Updated 3-Aug-12 20:26pm
v4
Comments
pradiprenushe 2-Aug-12 4:51am
   
Post source code for repeater.

Think about it for a minute. You have a loop. This is DIFFERENT to the loop the repeater is running in. But, inside that loop is a return statement. If you knew how to use a debugger, you'd have noticed that only the first iteration of this loop is ever run. So, you need to get rid of the loop, and instead pass in the data element to the method, so each row will work with the data from that row.

I believe you need to pass in Container.DataItem, and then cast it and find the data you want in your code from that row.
   
Comments
aminkourosh 2-Aug-12 4:45am
   
I know that my code is completely wrong . but i have any nice code for edit it.if you can help me i will be glad
Try to use the ajax call for your getimg() method and pass the ranking value as parameter and update your image source with Jquery..this solution will be easy for you..
OR
Create a DTO for your template and map that value to the Expression in Repeater.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900