Reputation: 666
I am using HyperLink inside the repeater control for showing categories and counting data rows, i want to add css class based on data row counting from code behind.
like this:
if (dt.Rows.Count > 10)
{
//add css class to 'HyperLink9' that is being used inside the repeater control
}
asp.net code
<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>
code behind
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();
}
on page load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindRepeaterData();
}
}
Upvotes: 0
Views: 1253
Reputation: 3949
What you'll want to do is add the ItemDataBound
handler to your repeater.
<asp:Repeater ID="CloudTags" runat="server"
OnItemDataBound="CloudTags_ItemDataBound">
This event will fire each time an item is bound to the repeater. Then, during the event, check the count of the items in the repeater. Note that the count of the items will be the count of the items already bound. Since you are currently in the process of binding an item, the count will be one less than you may think. If the count is greater than or equal to 10, find the hyperlink within that RepeaterItem
and add the 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";
}
}
}
Upvotes: 3