I have a Repeater inside a Grid View control (Telerik).
<telerik:GridTemplateColumn FilterControlAltText="Filter TemplateColumn column" HeaderText="<%$ resources: PARTNER_LIST_GRID_CATEGORY %>" UniqueName="TemplateColumn">
<HeaderStyle CssClass="myGridHeader" />
<ItemTemplate>
<asp:Repeater runat="server" ID="dlPartnerCat" OnItemDataBound="dlPartnerCat_OnItemDataBound">
<ItemTemplate>
<asp:Label runat="server" ID="lblPartCat" CssClass="k-margin-l-10-desktop" Text='<%#Eval("BusinessPartnerCategoryName")%>' />
</ItemTemplate>
<%-- <AlternatingItemTemplate>
<asp:Label runat="server" ID="lblPartCat" CssClass="k-margin-l-10-desktop" Text='<%#Eval("BusinessPartnerCategoryName")%>' />
</AlternatingItemTemplate>--%>
</asp:Repeater>
</ItemTemplate>
</telerik:GridTemplateColumn>
I bind the data to it in a following way:
void BindPartners()
{
var _partners = new BusinessPartnerRepo().GetAll(CompanyHelper.GetCompId()).Where(i => i.IsActive == true);
var _p = new TD.Data.Entity.BusinessPartner();
gvPartnerList.DataSource = _partners;
gvPartnerList.Rebind();
foreach (GridDataItem _dataItem in gvPartnerList.Items)
{
var _selectedItem = _dataItem.ItemIndex;
var _list = _dataItem.MuchBetterFindControl<Repeater>("dlPartnerCat");
var _id = (int)_dataItem.OwnerTableView.DataKeyValues[_selectedItem]["BusinessPartnerId"];
if (_id > 0)
{
_p = _partners.FirstOrDefault(i => i.BusinessPartnerId == _id);
if (_list != null)
{
_list.DataSource = _p.BusinessPartnerCategories.OrderBy(n => n.BusinessPartnerCategoryName);
_list.DataBind();
}
}
}
}
all of this works great. But I want to apply Css class to the label inside the repeater. This worked great with a single value (like I do with Status) and it even forks for the first value in the repeater..
protected void dlPartnerCat_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
foreach (GridDataItem _item in gvPartnerList.Items)
{
var _list = _item.MuchBetterFindControl<Repeater>("dlPartnerCat");
foreach (RepeaterItem _ite in _list.Items)
{
var _partCatlbl = _ite.MuchBetterFindControl<Label>("lblPartCat");
if (_partCatlbl != null)
{
switch (_partCatlbl.Text)
{
case "Customer":
_partCatlbl.CssClass = ApplicationCssHelper.CustomerClass;
break;
case "Customer - Supplier":
_partCatlbl.CssClass = ApplicationCssHelper.Customer_SupplierClass;
break;
case "Supplier":
_partCatlbl.CssClass = ApplicationCssHelper.SupplierClass;
break;
case "Agent":
_partCatlbl.CssClass = ApplicationCssHelper.AgentClass;
break;
default:
_partCatlbl.CssClass = ApplicationCssHelper.WarningClass;
break;
}
}
}
}
}
the value in the repeater can be multiple and I want to set different class based on the value (text) for the label in the repeater
any ideas anybody??
Ok, I have resolved this ...at least partially
I altered the OnDataBound method for the repeater:
protected void dlPartnerCat_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
foreach (GridDataItem _item in gvPartnerList.Items)
{
var _list = _item.MuchBetterFindControl<Repeater>("dlPartnerCat");
foreach (RepeaterItem _ite in _list.Items)
{
var _partCatlbl = _ite.MuchBetterFindControl<Label>("lblPartCat");
if (_partCatlbl != null)
{
switch (_partCatlbl.Text)
{
case "Customer":
_partCatlbl.CssClass = ApplicationCssHelper.CustomerClass;
break;
case "Customer - Supplier":
_partCatlbl.CssClass = ApplicationCssHelper.Customer_SupplierClass;
break;
case "Supplier":
_partCatlbl.CssClass = ApplicationCssHelper.SupplierClass;
break;
case "Agent":
_partCatlbl.CssClass = ApplicationCssHelper.AgentClass;
break;
default:
_partCatlbl.CssClass = ApplicationCssHelper.WarningClass;
break;
}
}
}
}
}
and the result are following:
as you can see all elements have css class applied exept the last one... anybody know why?
I think you need to remove both foreach
loops. There is no need to and it only add extra overhead.
protected void dlPartnerCat_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
var _partCatlbl = e.Item.FindControl("lblPartCat") as Label
if (_partCatlbl != null)
{
switch (_partCatlbl.Text)
{
case "Customer":
_partCatlbl.CssClass = ApplicationCssHelper.CustomerClass;
break;
case "Customer - Supplier":
_partCatlbl.CssClass = ApplicationCssHelper.Customer_SupplierClass;
break;
case "Supplier":
_partCatlbl.CssClass = ApplicationCssHelper.SupplierClass;
break;
case "Agent":
_partCatlbl.CssClass = ApplicationCssHelper.AgentClass;
break;
default:
_partCatlbl.CssClass = ApplicationCssHelper.WarningClass;
break;
}
}
}