将功能绑定到嵌入中继器的图像源 [英] bind a function to image source that embedded in repeater

查看:73
本文介绍了将功能绑定到嵌入中继器的图像源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个中继器,它内部有图像.我想基于排名显示不同的图像,例如,如果排名为1,则显示图像,如果排名为2,则显示其他图像,等等,并且我也具有5种图像和5级.

排名是数据集中的一列.但是我的功能无法正常工作,并且无法获得正确的结果.它只显示第一个图片.您对此操作有什么建议?

非常感谢.

这是我的代码

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

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";
     }

     }


及其html


and its html

<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>

推荐答案

请仔细考虑一下.你有一个循环.这与转发器运行所在的循环不同.但是,在该循环内部是一个return语句.如果您知道如何使用调试器,您将注意到仅运行了该循环的第一次迭代.因此,您需要摆脱循环,而是将data元素传递给方法,以便每一行都可以使用该行中的数据.

我相信您需要传递Container.DataItem,然后对其进行强制转换,并从该行中找到您想要的代码中的数据.
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.


尝试对getimg()使用ajax调用方法并将排名值作为参数传递,并使用Jquery更新图像源.此解决方案对您来说很容易.

为您的模板创建 DTO 并将该值映射到Repeater中的Expression.
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.


这篇关于将功能绑定到嵌入中继器的图像源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆