I have one Repeater
<asp:Repeater runat="server" ID="rptID" OnItemDataBound="repID_ItemDataBound">
<ItemTemplate>
<a href='example.com/somepage.aspx' id="myLink">
<%# Eval("MyVal")%>
</a>
</ItemTemplate>
</asp:Repeater>
In Code Behind I need to add one css class for this <a> tag when in repeater is one
Item
protected void repID_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
//how to set this class only then count of items is equal with 1
((HtmlGenericControl)e.Item.FindControl("myLink")).Attributes
.Add("class", "Count1");
}
}
This will give you the count of your datasource items:
if (((IEnumerable)rptID.DataSource).Cast<object>().Count() == 1)
{
((HtmlGenericControl)e.Item.FindControl("myLink")).Attributes
.Add("class", "Count1");
}
Counting the IEnumerable was borrowed from this thread: Calculating Count for IEnumerable (Non Generic)
Related
I have my repeater set up all working fine, but there's a few tweaks i need to make to every third item. my code is below
<asp:Repeater ID="rptItems" runat="server" onitemdatabound="rptItems_ItemDataBound">
<ItemTemplate>
content / html / eval
</ItemTemplate>
<AlternatingItemTemplate>
content / html / eval
</AlternatingItemTemplate>
</asp:Repeater>
then in my onitemdatabound set up
protected void rptItems_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
// check to see if this is the page to show the form on
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Visible = ((e.Item.Parent as Repeater).Items.Count % 3 == 0);
}
}
}
but this doesnt seem to work, it just hides one of the items, any help would be great.
You need to use item index not Count. Count is static value.
May be something like that:
e.Item.Visible = (e.Item.Index % 3 == 0);
use the RepeaterItem.ItemIndex instead of Items.Count
e.Item.ItemIndex % 3 == 0
so:
e.Item.Visible = e.Item.ItemType == ListItemType.AlternatingItem &&
e.Item.ItemIndex % 3 == 0
read more on MSDN
remove the Alternating item to do the modifications on every 3th item exclusively.
<asp:Repeater ID="rptItems" runat="server" onitemdatabound="rptItems_ItemDataBound">
<ItemTemplate>
content / html / eval
</ItemTemplate>
</asp:Repeater>
And your behind code would be:
e.Item.Visible = e.Item.ItemIndex % 3 == 0
I'm trying to get a column value of the first element on a repeater, and use it on the repeater's HeaderTemplate. After searching and trying many ways of achieving this through intellisense, I gave up and decided to post this question here.
My code is as follows:
Frontend
<asp:Repeater ID="states" runat="server">
<HeaderTemplate>
<h1>Stage: <asp:Literal ID="stageName" runat="server"></asp:Literal></h1>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li><%# DataBinder.Eval(Container.DataItem, "StateName") %></li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
Backend
protected void BindStageStates()
{
List<StagesStatesModel> statesList = App.Services.Stages.StagesService.GetStatesByStage(Page.DefaultApp, 1, Page.DefaultCultureId).Where(p => p.StateActive == true).ToList();
states.ItemDataBound += new RepeaterItemEventHandler(rptStagesStatesDataBound);
states.DataSource = statesList;
states.DataBind();
}
void rptStagesStatesDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
Literal stageName = (Literal)e.Item.FindControl("stageName");
stageName.Text = // Something to go here..
}
}
Thanks in advance!
Try OnItemCreated
protected void Repeater_OnItemCreated(Object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
e.Item.FindControl(ctrl);
}
if (e.Item.ItemType == ListItemType.Header)
{
e.Item.FindControl(ctrl);
}
}
By:
How to find controls in a repeater header or footer
im trying to change a value inside my repeater : (via itemdatabound event)
if the year is empty - set value blabla
my repeater :
<ItemTemplate>
<tr >
<td >
<%#Eval("year") %>
</td>
my c# code :
void RPT_Bordereaux_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (string.IsNullOrEmpty(((DataRowView)e.Item.DataItem)["year"].ToString()))
{
(((DataRowView)e.Item.DataItem)["year"]) = "blabla"; // ???????
}
}
it does change but not displayed in repeater ( the old value is displayed).
one solution is to add a server control or literal ( runat server) in the itemTemplate - and to "findControl" in server - and change its value.
other solution is by jQuery - to search the empty last TD.
but - my question :
is there any other server side solution ( ) ?
you can try something like this :
Repeater in .aspx:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<table>
<tr>
<td> <%# GetText(Container.DataItem) %></td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
.cs :
protected static string GetText(object dataItem)
{
string year = Convert.ToString(DataBinder.Eval(dataItem, "year"));
if (!string.IsNullOrEmpty(year))
{
return year;
}
else
{
return "blahblah";
}
}
IN GetText Method you can able to check by string that Empty or not than return string.
You could try to use the itemcreated event which occurs before the control is bound and not after the control is bound. Example in first link:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemcreated.aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater_events.aspx
ItemDataBound Occurs after an item in the Repeater control is data-bound but before it is rendered on the page.
HTML FILE
<asp:Repeater ID="RPT_Bordereaux" runat="server">
<ItemTemplate>
<table>
<tr>
<td> <%# GetValue(Container.DataItem) %></td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
.CS CODE
protected void RPT_Bordereaux_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
}
}
protected static string GetValue(object dataItem)
{
string year = Convert.ToString(DataBinder.Eval(dataItem, "year"));
if (!string.IsNullOrEmpty(year))
{
return Convert.ToString(year);
}
else
{
return "blahbla";
}
}
This should work
Lets say I have the following:
<asp:Repeater ID="repSubItems" runat="server" DataSource="<%# SubItems %>" >
<ItemTemplate>
<sc:FieldRenderer ID="FieldRenderer1"
FieldName="BlurbSpot_Content_SubHeading"
runat="server"
Item="<%# Container.DataItem as Sitecore.Data.Items.Item %>" />
</ItemTemplate>
</asp:Repeater>
I want to in code behind be able to do:
FieldRenderer1.Style["Width"] = MyCoolWidth;
But within the Repeater I cannot access the FieldRenderer1 control.
You will need to handle the ItemDataBound event of the repSubItems repeater. Example:
protected void repSubItems_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var fieldRenderer1 = e.Item.FindControl("FieldRenderer1") as Sitecore.Web.UI.WebControls.FieldRenderer;
if (fieldRenderer1 != null)
{
fieldRenderer1.Style["Width"] = MyCoolWidth;
}
}
}
You need to find the row your are looking for specifically in the Repeater and then find the control. Here is an example that can do it for all items in your Repeater:
// repeater item
foreach (Control cr in repSubItems.Controls)
{
// assuming this is your templated control name and not the final output name
FieldRenderer founcControl = cr.FindControl("FieldRenderer1") as FieldRenderer;
founcControl .Style["Width"] = MyCoolWidth;
}
The better way to do this would be to implement the OnDataBinding for your control specifically because then you have no searching to do:
<sc:FieldRenderer ID="FieldRenderer1" FieldName="BlurbSpot_Content_SubHeading"
runat="server" Item="<%# Container.DataItem as Sitecore.Data.Items.Item %>"
OnDataBinding="FieldRenderer1_DataBinding" />
protected void FieldRenderer1_DataBinding(object sender, System.EventArgs e)
{
FieldRenderer rend = (FieldRenderer)(sender);
// you can do whatever you want to rend at this point and it is scoped to ONLY
// the control so you never have to search for it.
rend.Style["Width"] = MyCoolWidth;
}
I know how to use a simple If statement wrapped in the <%# tags to hide something, but I don't know how to do it in a repeater when I need to access Container.DataItem, as in I need the dataItem currently being 'repeated'
eg
if (CurrentValidationMessage.Link != "")
{
show a hyperlink
}
Markup:
<asp:Repeater ID="repValidationResults" runat="server">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<a href='<%# ((MttImportValidationMessage)Container.DataItem).EditLink %>'> Link to erroneous Milestone </a>
<%# ((MttImportValidationMessage)Container.DataItem).Message %>
<br />
</ItemTemplate>
</asp:Repeater>
It might be more maintainable if you just tagged the controls in the repeater with id's and runat='server' and reference the DataItem in the ItemDataBound event by using e.Item.DataItem.
Then use e.Item.FindControl to reference your controls in the ItemTemplate and perform your logic.
protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
Domain.Employee employee = (Domain.Employee)e.Item.DataItem;
Control myControl = (Control)e.Item.FindControl("controlID");
//Perform logic
}
}
use ItemDataBound Event with repeater, and make the "a" tag with a runat="server" property and ID
protected void repValidationResults_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;
if (item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.Item)
{
HyperLink link = (HyperLink) item.FindControl("link");
//Do all your logic here :)
}
}
MarkUp:
<asp:Repeater ID="repValidationResults" runat="server">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<a runat="server" ID="link"> Link to erroneous Milestone </a>
<%# ((MttImportValidationMessage)Container.DataItem).Message %>
<br />
</ItemTemplate>
</asp:Repeater>