How to render two column layout using an asp.net repeater - asp.net

I have a list of items I want to render using a repeater, the twist is these items need to render in two columns in a table layout. Also if there are odd number of items the last cell will contain a place holder image. any thoughts?
Ex, my list [“string1”,”string2”,”string3”,”string4”,”string5”]
Display
<table>
<tr>
<td>string1</td>
<td>string2</td>
</tr>
<tr>
<td>string3</td>
<td>string4</td>
</tr>
<tr>
<td>string5</td>
<td>string6</td> or [<td>Some place holder Imge if odd number </td>]
</tr>
</table>
So that I end up with a view that looks like this
“String1” “String2”
“String3” “String4”
“String5” “String6”
Or if the list has odd number of Items
“String1” “String2”
“String3” “String4”
“String5” “Some place holder image”

We can render two columns in markup. Also we have to add a placeholder that we will use in code to insert image :
<asp:Repeater ID="rptMyRepeater" runat="server" OnItemDataBound="rptMyRepeater_ItemDataBound">
<HeaderTemplate>
<table>
<tr>
</HeaderTemplate>
<ItemTemplate>
<%# (Container.ItemIndex != 0 && Container.ItemIndex % 2 == 0) ? #"</tr><tr>" : string.Empty %>
<%# string.Format("{0}{1}{2}", #"<td>", Container.DataItem, #"</td>") %>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</ItemTemplate>
<FooterTemplate>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
Now in the code check for last item and check if it is odd. If it is, add an image inside td, append the td in placeholder:
protected void rptMyRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
int count = ((List<string>)rptMyRepeater.DataSource).Count;
if (e.Item.ItemIndex != 0 && e.Item.ItemIndex % 2 == 0 && e.Item.ItemIndex == count - 1)
{
PlaceHolder PlaceHolder1 = e.Item.FindControl("PlaceHolder1") as PlaceHolder;
Image img = new Image();
img.ImageUrl="pholder.jpg";
TableCell td = new TableCell();
td.Controls.Add(img);
PlaceHolder1.Controls.Add(td);
}
}
}
And the code I have used for testing:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//rptMyRepeater.DataSource = new List<String>() { "String1", "String2", "String3", "String4", "String5", "String6" };
rptMyRepeater.DataSource = new List<String>() { "String1", "String2", "String3", "String4", "String5" };
rptMyRepeater.DataBind();
}
}
Hope it helps!

Related

How do I display child footer of a repeater if the child repeater is empty?

I have two repeaters nested in my application which is working fine. I would love to display the footer if the child repeater is empty. Due to my code my long html, i will just drop a sample of what my html looks like and post my full code for better understanding. Everything works fine though unless when the child repeater is empty i want to display the footer message
<asp:Repeater ID="ProductRepeater" runat="server" OnItemDataBound="ProductRepeater_ItemDataBound">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td>
<%#Eval("Name")%>
</td>
<tr>
</table>
<pre>
<asp:Repeater ID="ChildRepeater" runat="server">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td>
<%#Eval("Description")%>
<td>
</tr>
<table>
</itemTemplate>
<FooterTemplate>
<div>
<h5>
<asp:Label ID="lblDefaultMessage" runat="server" Text="This is empty. " Visible="false" ForeColor="Red" Font-Size="Large">
</asp:Label>
</h5>
</div>
</FooterTemplate>
</asp:Repeater>
</pre>
</ItemTemplate>
</asp:Repeater>
<protected void ProductRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string category = "Value"
Repeater rptRelated = e.Item.FindControl("ChildRepeater") as Repeater;
rptRelated.DataSource = getChild(category);
rptRelated.DataBind();
if (rptRelated.Items.Count < 1)
{
if (e.Item.ItemType == ListItemType.Footer)
{
Label lblDefaultMessage = (Label)e.Item.FindControl("lblDefaultMessage");
lblDefaultMessage.Visible = true;
}
}
}
}
Repeater has no EmptyDataTemplate like GridView.
You should place your footer content next to Repeater and change your "databinding" code like this:
Repeater rptRelated = e.Item.FindControl("ChildRepeater") as Repeater;
Label lblDefaultMessage = (Label)e.Item.FindControl("lblDefaultMessage");
var ds = getChild(category);
lblDefaultMessage.Visible = ds != null && ds.Rows.Count != 0;
if (rptRelated != null)
{
rptRelated.DataSource = ds;
rptRelated.DataBind();
}
Find Repeater like:
// find Child Repeater
Control ctrl = (sender) as Control;
Repeater rptRelated = ctrl.Parent.NamingContainer as Repeater;
And Find control from Repeater's Footer like:
Label lblDefaultMessage =
(Label)rptRelated.Controls[rptRelated.Controls.Count-1].FindControl("lblDefaultMessage");
Add this into child Repeater's OnItemDataBound event instead of parent.
protected void ChildRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Control ctrl = (sender) as Control;
Repeater rptRelated = ctrl.Parent.NamingContainer as Repeater;
if (rptRelated.Items.Count < 1)
{
if (e.Item.ItemType == ListItemType.Footer)
{
Label lblDefaultMessage = (Label)rptRelated.Controls[rptRelated.Controls.Count-1].FindControl("lblDefaultMessage");
lblDefaultMessage.Visible = true;
}
}
}
Event is:
<asp:Repeater ID="ChildRepeater" runat="server"
OnItemDataBound="ChildRepeater_ItemDataBound">

Repeater inside repeater not binding properly

I have a repeater inside another. Like so :
<asp:Repeater ID="CategoryRepeater" runat="server" OnItemDataBound="ItemBound">
<ItemTemplate>
<div class="groupbox">
<fieldset>
<legend><%# Container.DataItem %></legend>
<table>
<asp:Repeater ID="ItemRepeater" runat="server">
<ItemTemplate>
<tr>
<td>
<asp:CheckBox id="chkItem" runat="server" Text='<%# Eval("Text")%>' />
<asp:Button ID="btnXRefs" Text="x-refs" runat="server" CssClass="xRefButton" OnClick="btnSelectXRefs_Click" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</fieldset>
</div>
</ItemTemplate>
</asp:Repeater>
CODE BEHIND :
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
using (var db = new DbContext())
{
CategoryRepeater.DataSource = db.Categories.OrderBy(x => x.Heading).Select(x => x.Heading).Distinct();
CategoryRepeater.DataBind();
}
}
}
protected void ItemBound(object sender, RepeaterItemEventArgs args)
{
if (args.Item.ItemType == ListItemType.Item)
{
Repeater childRepeater = (Repeater)args.Item.FindControl("ItemRepeater");
var item = args.Item as RepeaterItem;
using (var db = new DbContext())
{
childRepeater.DataSource = db.Categories.Where(x => x.Heading == item.DataItem).OrderBy(x => x.Text);
childRepeater.DataBind();
}
}
}
My goal is to first make many groupboxes using the top level, then bind items into each one. So I end up with many small stacked lists of checkboxes.
Problem is, all the top level boxes appear, yet only the first one contains checkbox items, ie. only the first one is bound internally, and the ItemBound method is only called once for the first one.
Any ideas why?
This line
if (args.Item.ItemType == ListItemType.Item)
Should be like this
if(args.Item.ItemType = ListItemType.Item ||
args.Item.ItemType == ListItemType.AlternatingItem)

Why is my ASP.NET Repeater data not available in load events via code behind?

I have an ASP.NET repeater that is being populated via a datasource. The data loads and presents perfectly on the screen, but all attempts to access any row of data in that repeater via code behind during the load phase are unsuccessful. I can access everything perfectly on a form submit, once the repeater has rendered on the screen, but in every load-related event I have tried (Load, Init, PreRender, DataBind, etc), it shows as empty. Is it necessary to populate the repeater programatically in code behind, rather than ASPX markup, in order to access this data during the load phase or am I just doing something wrong?
ASPX snippet:
<div runat="server" id="rpt3" style="display: block;">
<p class="approvalHeaderText">
Rejected Items</p>
<asp:Repeater ID="RptRejected" runat="server" DataSourceID="SQL_EmployeeGetRejectedEdits">
<ItemTemplate>
<tr class="gridToggleWhite gridVisible" id="<%# Eval("RecID") %>_cont">
<td>
<%# Eval("OrderID")%>
</td>
Code behind snippet - tried in several events (assume all variables have been declared):
For i = 0 To RptRejected.Items.Count - 1
'Obtain current week's Expenses
curExpVal = RptRejected.Items(i).FindControl("ExpensePay")
If Not IsNothing(curExpVal) Then
If curExpVal.Text = "" Then
insertExp = 0
Else
insertExp = CSng(curExpVal.Text)
End If
Else
insertExp = 0
End If
Next
You can use the ItemDataBound event (and you don't need the loop):
Sub rpt3_ItemDataBound(Sender As Object, e As RepeaterItemEventArgs)
If (e.Item.ItemType = ListItemType.Item) OrElse _
(e.Item.ItemType = ListItemType.AlternatingItem) Then
Dim curExpVal = e.Item.FindControl("ExpensePay")
If Not IsNothing(curExpVal) Then
If curExpVal.Text = "" Then
insertExp = 0
Else
insertExp = CSng(curExpVal.Text)
End If
Else
insertExp = 0
End If
End If
End Sub
R1_Load and R1_ItemDataBound should be used to populate your data and R1_ItemCommand should be used to handle the button actions.
aspx
<asp:Repeater runat="server" ID="R1" OnItemCommand="R1_ItemCommand" OnItemDataBound="R1_ItemDataBound"
OnLoad="R1_Load">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:TextBox runat="server" ID="txt"></asp:TextBox><asp:Button runat="server" ID="btn"
Text="Add" CommandName="AddOne" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table> </FooterTemplate>
</asp:Repeater>
aspx.cs
protected void R1_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
var items = new List<int>();
for (int i = 0; i < 5; i++)
{
items.Add(i);
}
R1.DataSource = items;
R1.DataBind();
}
}
protected void R1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var txt = e.Item.FindControl("txt") as TextBox;
txt.Text = Convert.ToString(e.Item.DataItem);
}
}
protected void R1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "AddOne")
{
var txt = e.Item.FindControl("txt") as TextBox;
txt.Text = (Convert.ToInt32(txt.Text) + 1).ToString();
}
}
My key mistake was using Eval() instead of the DataBinder.Eval() method. While the result is the same on the screen, Eval() does not trip the OnItemDataBound event. Once I switched my markup over to DataBinder.Eval(), everything fell into place. Thanks, everyone, for your ideas.

nested repeater show data that matches parent repeater

I am using a repeater to build a custom table. However, I can't figure out how to make the table show subtotal if the next rows tour does not match the previous rows.
Something similar to that.
row1 tour1
row2 tour 1
tour1 subtotal
row3 tour2
row4 tour2
subtotal
total
<asp:Repeater ID="ParentRepeater" runat="server" DataSourceID="SqlDataSource1">
<HeaderTemplate>
<table border="1">
<tr>
<th>TOUR</th>
<th>THEME</th>
<th>ROUTE</th>
<th>DEPT</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Container.DataItem("tour")%></td>
<td align="center"><%#Container.DataItem("theme")%></td>
<td align="right"><%#Container.DataItem("route")%></td>
<td align="right"><%#Container.DataItem("dep7")%></td>
<asp:Repeater ID="ChildRepeater" runat="server"
DataSourceID="SqlDataSource2">
<HeaderTemplate>
<table border="1">
<tr>
<th>BOOKNO</th>
<th>PARTY</th>
<th>TOUR</th>
<th>THEME</th>
<th>ROUTE</th>
<th>DEPT</th>
<th>HOME</th>
<th>USERID</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td align="center"><%#Container.DataItem("bookno") %></td>
<td><%#Container.DataItem("party")%></td>
<td><%#Container.DataItem("tour")%></td>
<td align="center"><%#Container.DataItem("theme")%></td>
<td align="right"><%#Container.DataItem("route")%></td>
<td align="right"><%#Container.DataItem("dep7")%></td>
<td align="right"><%#Container.DataItem("home")%></td>
<td align="right"><%#Container.DataItem("userid")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
code behind
Protected Sub ItemBound(ByVal sender As Object, ByVal args As RepeaterItemEventArgs)
If args.Item.ItemType = ListItemType.Item Then
Dim childRepeater As Repeater = DirectCast(args.Item.FindControl("ChildRepeater"), Repeater)
childRepeater.DataSource = SqlDataSource2
childRepeater.DataBind()
End If
End Sub
but this shows all the data in the nested repeater not the ones that match the parentrepeater fields
for example tour,theme,dep7 should match in the child repeater
First, add 2 class fields:
private int _subTotal; // Not sure what you're summing but you
// wrote subtotal in your pseudo-output
private int _lastTourId;
Reset these field before you databind your repeater:
_subTotal = 0;
_lastTourId = -1;
TourRepeater.DataBind();
The create a single level Repeater:
<asp:Repeater ID="TourRepeater" ...>
<HeaderTemplate>...<>
<ItemTemplate>
<tr>
Some columns with your tour data
</tr>
<PlaceHolder ID="SubTotalRow" ... />
</ItemTemplate>
<FooterTemplate>
<PlaceHolder ID="SubTotalRow" ... />
</FooterTemplate>
</asp:Repeater>
Bind this Repeater directly to your tour data, then do this in your OnItemDataBound event handler (I'll have to go for c#):
void RepeaterItemDataBound(object source, RepeaterItemEventArgs e)
{
if (
e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem // think you forgot this
)
{
var tour = e.Item.DataItem as Tour;
if (tour != null)
{
if (_lastTourId == -1) { _lastTourId == tour.TourId; } // To avoid subtotal row before first tour
if (tour.TourId != _lastTourId) // This is a new tour so insert a subtotal row for the previous tour
{
var subTotalRow = e.Item.FindControl("SubTotalRow") as PlaceHolder;
if (subTotalRow != null)
{
RenderSummaryRow(subTotalRow, _subTotal);
}
_subTotal = tour.SomeValueYouWantToAddToSubTotal;
}
else
{
_subTotal += tour.SomValueYouWantToAddToSubTotal;
}
_lastTourId = tour.TourId;
}
}
else if (e.Item.ItemType == ListItemType.Footer)
{
var subTotalRow = e.Item.FindControl("SummaryRow") as PlaceHolder;
if (summaryTotalRow != null)
{
RenderSummaryRow(subTotalRow, _subTotal);
}
}
}
private void RenderSummaryRow(PlaceHolder placeHolder, int subTotal)
{
// create subtotal row manually by either creating TableRow + TableCells and adding them to a placeholder, or
// just add a literal and create your markup manually as innerHtml. Put your _subtotal value in where you want
}
An alternative method is to retrieve your tour data into a DataTable, and retrieve the data WITH ROLLUP etc which will create summary rows automagically. There's several tutorials about this around the web, but I couldn't find the great one I used before I switched to MVC.

Injecting table row inside repeater item

I am trying to inject inside repeaters items, the problem is that the row is generated in a wrong location.
If you try this simple example, you will see that the rown is generated under label '2' as it should be generated under label '1'.
Why does it happen? And how to fix that?
protected void Page_Load(object sender, EventArgs e)
{
int [] array = {1,2,3,4,5};
rpt.DataSource = array;
rpt.DataBind();
}
protected string _extraRowHtml;
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
int tmp = ((int)e.Item.DataItem);
_extraRowHtml = tmp == 1 ? "<tr><td class=\"topicsRow\" colspan=\"100%\"> Topics </td></tr>" : string.Empty; ;
}
}
<asp:Repeater ID="rpt" runat="server" onitemdatabound="rpt_ItemDataBound">
<HeaderTemplate>
<table cellpadding="0px" cellspacing="0">
</HeaderTemplate>
<ItemTemplate>
<tr style="height:50px">
<td>
<asp:HyperLink ID="lnkTitle" runat="server" Text='<%# Container.DataItem%>'/>
</td>
</tr>
<%# _extraRowHtml %>
</ItemTemplate>
<FooterTempl
</table>
</FooterTemplate>
</asp:Repeater>
Personally I think a better way of doing is is to replace:
<%# _extraRowHtml %>
with
<%# GetExtraRow(Container.DataItem) %>
then in your code behind implement:
protected string GetExtraRow(object Data)
{
int tmp = (int) Data:
return _tmp == 1 ?
"<tr><td class=\"topicsRow\" colspan=\"100%\"> Topics </td></tr>" :
string.Empty;
}
Then and get rid of the rpt_ItemDataBound function (and the onItemDataBound).
Doing things this way is going to be prone to error, as using a variable in this manner is not something that you are going to have full control over.
What I would do is add something like the following to the template.
<asp:literal id="litExtraRow" runat="server" mode="Passthrough" />
Then in your codebehind on the item databound event
if (((int)e.Item.DataItem) == 1)
{
((Literal)e.Item.FindControl("litExtraRow")).Text = "Your HTML Here";
}
Something like this should be a bit more reliable.
The reason you are having issues is that the template is evaluated with the values as they are as "ItemDataBound" has been called, and row 1, the value is string.Empty, and then for the second row, you updated it after item 1 was databound.

Resources