简体   繁体   English

如何在asp.net中的中继器控件中计算最后一列的值

[英]how to count value of last column in repeater control in asp.net

Look at the below picture I want to count value of last column (cnt) I am using repeater control. 看下面的图片,我要计算我正在使用中继器控件的最后一列(cnt)的值。

example: 例:

if (last column (cnt) is greater than 5> )
{
    response.write ("6");
}

sql query sql查询

在此处输入图片说明

 SELECT id, category, ( SELECT COUNT(id) FROM entry_table WHERE category.id = entry_table.Cat_id) as cnt FROM category

Repeater code 中继器代码

<asp:Repeater ID="CloudTags" runat="server"  OnItemDataBound="CloudTags_ItemDataBound">
    <ItemTemplate>
        <asp:HyperLink ID="HyperLink9" runat="server">
             <%#DataBinder.Eval(Container,"DataItem.Category")%>
            (<%#DataBinder.Eval(Container,"DataItem.cnt")%>)
        </asp:HyperLink>
    </ItemTemplate>
</asp:Repeater>

Code behind : 后面的代码

protected void CloudTags_ItemDataBound(object sender, RepeaterItemEventArgs e)
{

}

You could do something like: 您可以执行以下操作:

protected void CloudTags_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    var repeaterItem = e.Item;
    // TODO you still have to check the type of the repeaterItem
    var dataItem = (dynamic) repeaterItem.DataItem;
    var cnt = dataItem.cnt;
    if (cnt > 5)
    {
        var hyperLink = (HyperLink) repeaterItem.FindControl("HyperLink9");
        hyperLink.CssClass = "TagSize2";
    }
}

or 要么

protected void CloudTags_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    var repeaterItem = e.Item;
    // TODO you still have to check the type of the repeaterItem
    var dataItem = repeaterItem.DataItem;
    var objCnt = DataBinder.Eval(dataItem, "cnt");
    // TODO check the whole parsing/converting stuff ...
    var stringCnt = objCnt.toString();
    var cnt = int.Parse(stringCnt);
    if (cnt > 5)
    {
        var hyperLink = (HyperLink) repeaterItem.FindControl("HyperLink9");
        hyperLink.CssClass = "TagSize2";
    }
}

Anyway, I strongly suggest you to not use dynamic or DataBinder.Eval and rather cast the .Data -property to a strong type! 无论如何,我强烈建议您不要使用dynamicDataBinder.Eval ,而是将.Data属性转换为强类型! Otherwise, this will cause some fancy runtime reflection and will have an impact on the performance! 否则,这会引起一些花哨的运行时反射,并会影响性能!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在asp.net中的中继器控件列中显示图像? - how to show image in the column of repeater control in asp.net? 在Repeater ASP.NET控件中以所需样式动态显示列名称和列值 - Display column name and column value dynamically in desired style in Repeater ASP.NET control 在嵌套转发器控件中显示数据以及如何使用asp.net将值ParentID从父转发器传递到子转发器 - Display data in Nested repeater control & how to pass value parentID from Parent Repeater to Child Repeater using asp.net ASP.NET转发器控件-在转发器控件内获取Hiddenfield值 - ASP.NET Repeater Control - Getting Hiddenfield value inside the repeater control 如何将我在中继器中选择的 DropDownList 值添加到 ASP.Net 中的列表框控件 - How Can add I selected value of DropDownList inside a Repeater to the listbox control in ASP.Net 如何在ASP.Net中的Repeater控件中添加分页功能? - How to add paging capabilities to the Repeater control in ASP.Net? 如何将rss绑定到asp.net 2.0中的转发器控件 - how to bind rss to a repeater control in asp.net 2.0 ASP.NET如何使用Repeater控件展示产品 - how to display products using Repeater control ASP.NET 如何将asp.net转发器控件动态绑定到数据源 - How to dynamically bind asp.net repeater control to datasource 如何在 ASP.NET 中使用带有中继器控件的分页? - How to use paging with Repeater control in ASP.NET?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM