Accessing a control within a Repeater - c#

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;
}

Related

C# Get first repeater item column value to use on HeaderTemplate

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

Binding controls dynamically to PlaceHolder in asp:DataList

I can't seem to figure this one out but I'm trying to add a user control to a DataList at runtime (because the actual control type can differ). So if I hard code the control reference in the markup like this it works:
<asp:DataList ID="myDL" runat="server" RepeatDirection="Horizontal" RepeatColumns="3" OnItemDataBound="myDL_Item_Bound">
<ItemTemplate>
<prefix:MyControl ID="myControl1" runat="server" />
</ItemTemplate>
</asp:DataList>
But if I try to add it programmatically to a placeholder, it does not render the user controls (just empty td tags):
<asp:DataList ID="myDL" runat="server" RepeatDirection="Horizontal" RepeatColumns="3" OnItemDataBound="myDL_Item_Bound">
<ItemTemplate>
<asp:PlaceHolder ID="ph" runat="server">
</asp:PlaceHolder>
</ItemTemplate>
</asp:DataList>
protected void myDL_Item_Bound(Object sender, DataListItemEventArgs e) {
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
PlaceHolder ph = (PlaceHolder)e.Item.FindControl("ph");
if (ph != null) {
MyControl ctrl = new MyControl();
ctrl.SomeProp = "xyz";
ph.Controls.Add(ctrl);
}
else {
MyControl ctrl = (MyControl)e.Item.FindControl("myControl1");
ctrl.SomeProp = "xyz";
}
}
}
what am I missing?
You are not adding the control to the page. You need to add it:
Control ctrl = (Control)Page.LoadControl("MyControl.ascx");
// MyControl ctrl = new MyControl();
ctrl.SomeProp = "xyz";
ph.Controls.Add(ctrl);

item count of datalist that is inside a datalist

I'm trying to get the item count from a datalist that sits inside a datalist. I thought this is how I would do it but its returning null. (aspx code condensed for readability)
<asp:DataList id="searchResultsProductDataList" runat="server" >
<asp:DataList ID="productDataList" runat="server">
</asp:DataList>
</asp:DataList>
Here is the code-behind
DataList resultnumberDL = (DataList)e.Item.FindControl("productDataList");
LiteralTest.Text = resultnumberDL.Items.Count.ToString()
I've also tried
DataList resultnumberDL = ((DataList)FindControl("productDataList"));
LiteralTest.Text = resultnumberDL.Items.Count.ToString()
This is how I would go about doing this right?
This can be done like this in DataList1_ItemDataBound
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
Label PriceLabel = (Label)e.Item.FindControl("PriceLabel");
Label SalePrice = (Label)e.Item.FindControl("SalePrice");
//
// Do you calculations here ..
//
SalePrice.Text = "Your Final Value";
}
}
Maybe double-check your syntax...
If your ASP.NET controls are structured like this:
<asp:DataList ID="dl1" runat="server" onitemdatabound="dl1_ItemDataBound">
<ItemTemplate>
...
<asp:DataList ID="dl2" runat="server" Enabled="true">
<ItemTemplate>
...
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
and your C# code-behind for the nested DataList like this:
protected void dl1_ItemDataBound(object sender, DataListItemEventArgs e)
{
DataList dl2 = (DataList)e.Item.FindControl("dl2");
... // load DataTable
dl2.DataSource = dt;
dl2.DataBind();
}
in this case e.Item.FindControl("[id]") will find your nested DataList

Hide an element in ASP.net based on an if inside a Repeater

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>

Databinding a List to a usercontrol in an Item Template in codebehind

I have a DataList like below:
<asp:DataList runat="server" ID="myDataList">
<ItemTemplate>
<uc:MyControl ID="id1" runat="server" PublicProperty='<%# Container.DataItem %>' />
</ItemTemplate>
</asp:DataList>
The Item Template is simply a registered usercontrol, MyControl. The DataSource for the DataList is a List<List<T>> and MyControl's PublicProperty is passed List<T> which it then peforms its own databinding on. This works fine, but I have a general aversion to databinding in the aspx/c page. What is the most efficent way to set the PublicProperty value in the code behind?
If in line data binding syntax is not good enough for you - you can always hook into the ItemDatabound event of the DataList.
<asp:DataList runat="server" ID="myDataList"
OnItemDataBound="DataList_ItemDataBound">
<ItemTemplate>
<uc:MyControl ID="id1" runat="server" />
</ItemTemplate>
</asp:DataList>
Then, in the code behind of your page/containing control you can add your ItemDataBound event.
protected void DataList_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item
|| e.Item.ItemType == ListItemType.AlternatingItem)
{
DataListItem item = e.Item;
//List<string> or whatever your data source really is...
List<string> dataItem = item.DataItem as List<string>;
MyControl lit = (MyControl)item.FindControl("id1");
lit.PropertyName = dataItem;
}
}
For more information on the DataList.ItemDataBound event - Read Here
If you would rather not declare your ItemDataBound delegate inline in the ASPX you could also do it in the code behind - probably in your Page Load event:
myDataList.ItemDataBound += DataList_ItemDataBound;
Hope that helps

Categories

Resources