gpt4 book ai didi

c# - 如何根据数据行计数从代码后面将css类添加到 'hyperlink'内部的 'Repeater control'

转载 作者:太空宇宙 更新时间:2023-11-03 23:30:51 25 4
gpt4 key购买 nike

我在转发器控件中使用 HyperLink 来显示类别和计算数据行,我想添加基于代码后面的数据行计数的 css 类。

像这样:

if (dt.Rows.Count > 10)
{

//add css class to 'HyperLink9' that is being used inside the repeater control

}

asp.net代码

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

代码隐藏

protected void BindRepeaterData()
{

con.Open();

SqlDataAdapter da = new SqlDataAdapter("SELECT id, category, ( SELECT COUNT(id) FROM entry_table WHERE category.id = entry_table.cat_id) as cnt FROM category", con);
DataTable dt = new DataTable();
da.Fill(dt);

CloudTags.DataSource = dt;

if (dt.Rows.Count > 10)
{

//i want to add css class here if row count is greater than 10 in 'HyperLink9'

}

CloudTags.DataBind();

con.Close();

}

页面加载

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindRepeaterData();
}

}

最佳答案

您需要做的是将 ItemDataBound 处理程序添加到您的转发器。

<asp:Repeater ID="CloudTags" runat="server"
OnItemDataBound="CloudTags_ItemDataBound">

每次将元素绑定(bind)到转发器时都会触发此事件。然后,在事件期间,检查中继器中的元素数量。请注意,元素的数量将是已经绑定(bind)的元素的数量。由于您当前正在绑定(bind)一个元素,因此计数会比您想象的少一个。如果计数大于或等于 10,则在该 RepeaterItem 中找到超链接并添加 CssClass

protected void CloudTags_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (CloudTags.Items.Count >= 10)
{
HyperLink HyperLink9 = (HyperLink)e.Item.FindControl("HyperLink9");
HyperLink9.CssClass = "some-class";
}
}
}

关于c# - 如何根据数据行计数从代码后面将css类添加到 'hyperlink'内部的 'Repeater control',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25405324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com