I am using nested repeaters. I render several groups and every group has members that I need to render out different properties to.
I am using ItemDataBound on the nested repeater to find out which member the nested repeater is rendering out at the moment. I have <% %> tags in the .aspx which checks if the current user has a certain property, and if it has, I render the divs and other HTML elements that need to go there.
All the data is correct. When I debug I collect the correct data and I get the right info about who the user is and what properties the user has. I save the current evaluated user in a protected string currentRenderedUser that I use to find out who the user is in the aspx file.
The problem is that by the time the <% %> on the aspx gets evaluated the repeater has already gone through all the sets or something, because the currentRenderedUser is the same during every evalutaion in the aspx file.
I don't know the correct terminology for "<% %>" code in the aspx file, sorry.
This is the relevant parts of the aspx (I think):
<ItemTemplate>
<!-- nested repeater data -->
<tr>
<td width="50%"><div class="memberName"><%# DataBinder.Eval(Container.DataItem, "userEmail" )%></div></td>
<div style="display: none;"></div>
<% if (CheckIfUserIsAdmin(email, currentRenderedGroup))
{ %>
<%if (CheckIfUserIsAdmin(currentRenderedUser, currentRenderedGroup))
{ //Checks so that the user being rendered isn't already admin%>
<td><div style="margin-right:30px;" class="makeAdminButton"></div></td>
<% }
else
{%>
<td><div style="margin-right:30px;" class="makeAdminButton"><asp:ImageButton ID="ImageButtonMakeUserAdmin" CommandName="MakeAdmin" BorderWidth="0" ImageUrl="~/gfx/doAdmin.png" CommandArgument='<%#Convert.ToString(DataBinder.Eval(Container.DataItem, "userEmail" )) + " " + Convert.ToString(DataBinder.Eval(((RepeaterItem)Container.Parent.Parent).DataItem, "groupId"))%>' runat="server" AlternateText="Make admin" /></div></td>
<td><div class="removeUserButton"><asp:ImageButton ID="ImageButtonRemoveUserFromGroup" CommandName="RemoveUserFromGroup" BorderWidth="0" ImageUrl="~/gfx/can.png" CommandArgument='<%# Convert.ToString(DataBinder.Eval(Container.DataItem, "userEmail" )) + " " + Convert.ToString(DataBinder.Eval(((RepeaterItem)Container.Parent.Parent).DataItem, "groupId"))%>' runat="server" AlternateText="Delete" /></div></td>
<% } %>
<%}%>
</tr>
</ItemTemplate>
And this is the part of the back-end that evaluates the current user:
protected void NesterRepeater_ItemDataBound(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem
|| e.Item.ItemType == ListItemType.Item)
{
DataRowView dataRowView = (DataRowView)e.Item.DataItem;
string memberInGroup = Convert.ToString(dataRowView["userEmail"]);
currentRenderedUser = memberInGroup;
}
}
How can I solve this in a better (or atleast, working) way?
Thanks
The problem is that the event ItemDataBound is executed first so you probably always get the value of the last record from the Repeater's DataSource if I'm not mistaken.
It is better to do it this way:
<ItemTemplate>
<tr>
<td width="50%"><div class="memberName"><%# DataBinder.Eval(Container.DataItem, "userEmail" )%></div></td>
<div style="display: none;"></div>
<asp:PlaceHolder ID="phNoAdmin" runat="server" Visible="false">
<td><div style="margin-right:30px;" class="makeAdminButton"></div></td>
</asp:PlaceHolder>
<asp:PlaceHolder ID="phAdmin" runat="server" Visible="false">
<td><div style="margin-right:30px;" class="makeAdminButton"><asp:ImageButton ID="ImageButtonMakeUserAdmin" CommandName="MakeAdmin" BorderWidth="0" ImageUrl="~/gfx/doAdmin.png" CommandArgument='<%#Convert.ToString(DataBinder.Eval(Container.DataItem, "userEmail" )) + " " + Convert.ToString(DataBinder.Eval(((RepeaterItem)Container.Parent.Parent).DataItem, "groupId"))%>' runat="server" AlternateText="Make admin" /></div></td>
<td><div class="removeUserButton"><asp:ImageButton ID="ImageButtonRemoveUserFromGroup" CommandName="RemoveUserFromGroup" BorderWidth="0" ImageUrl="~/gfx/can.png" CommandArgument='<%# Convert.ToString(DataBinder.Eval(Container.DataItem, "userEmail" )) + " " + Convert.ToString(DataBinder.Eval(((RepeaterItem)Container.Parent.Parent).DataItem, "groupId"))%>' runat="server" AlternateText="Delete" /></div></td>
</asp:PlaceHolder>
</tr>
</ItemTemplate>
And the code behind:
protected void NesterRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
DataRowView dataRowView = (DataRowView)e.Item.DataItem;
PlaceHolder phNoAdmin = e.Item.FindControl("phNoAdmin") as PlaceHolder;
PlaceHolder phAdmin = e.Item.FindControl("phAdmin") as PlaceHolder;
string memberInGroup = Convert.ToString(dataRowView["userEmail"]);
currentRenderedUser = memberInGroup;
if (CheckIfUserIsAdmin(email, currentRenderedGroup))
{
if (CheckIfUserIsAdmin(currentRenderedUser, currentRenderedGroup))
{
phNoAdmin.Visible = true;
phAdmin.Visible = false;
}
else
{
phNoAdmin.Visible = false;
phAdmin.Visible = true;
}
}
}
}
It would be better if you bind the data inside the 2 PlaceHolder control I wrote also in code behind for maintainability reason.
HTH
Related
I have a template:
<asp:Repeater ID="litFolder" runat="server" OnItemDataBound="litFolder_ItemDataBound">
<HeaderTemplate>
<ul class="test" id="currentLink">
</HeaderTemplate>
<ItemTemplate>
<div class="leftNav">
<li>
<asp:HyperLink ID="innerHyperLink" runat="server"></asp:HyperLink>
</li>
</div>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
and I'm trying to set the to display block when a link is selected. I can set the link to display block, but how do I set the ul to display block ( only using C# )
protected void litFolder_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
// if the child from the first repeater has children, it will grab them here
Item innerItem = (Item)e.Item.DataItem;
if (innerItem != null)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// this creates a link to the page in sitecore once clicked
HyperLink topNavigation = (HyperLink)e.Item.FindControl("innerHyperLink");
topNavigation.NavigateUrl = LinkManager.GetItemUrl(innerItem);
topNavigation.Text = innerItem["Title"];
if (topNavigation != null) {
//this is where I think I need to define the ul to display block
}
}
}
}
I need to make sure that the current link sets the ul that it is in and not all the ul's with the class test.
In order to prevent the Ul from being broken, place the <div class="leftNav"> inside the <li> as shown below:
<asp:Repeater ID="litFolder" runat="server" OnItemDataBound="litFolder_ItemDataBound">
<HeaderTemplate>
<ul class="test" id="currentLink">
</HeaderTemplate>
<ItemTemplate>
<li>
<div class="leftNav">
<asp:HyperLink ID="innerHyperLink" runat="server"></asp:HyperLink>
</div>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
To set the display block style, you can find the control based on the ID, then set the style to it.
Thanks
I have the following data-bound repeater code :
<%--categories--%>
<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:HiddenField id="pgNos" runat="server" Value='<%# Eval("PGNos")%>' />
<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>
There is a repeater inside a repeater. How do I access the text inside the legend (<legend><%# Container.DataItem %></legend>) from code?
I tried :
foreach (RepeaterItem cr in CategoryRepeater.Items)
{
string heading = (string) cr.DataItem; // returns null
}
Container.DataItem is a runtime alias for the DataItem for this specific item in the bound list. For a Repeater which displays 10 rows of data, this is one row from the datasource...Basically, it's a specific row and at runtime you can get the Property Values from this row
I saw your above Mark Up...Seems like you are missing to mention the property of Data-Bound type Class like below.
<%# ((Your Class Name)Container.DataItem).Class Property Name %>
There is a repeater inside a repeater. How do I access the text inside
the legend (<%# Container.DataItem %>) from code?
As told by phemt.latd, you can change the Legend tag into server side control like below.
<legend id="lg" runat="server">
<%# ((Your Class Name)Container.DataItem).Class Property Name %>
</legend>
Now, in the Item-Bound Data Event, Find the Legend Control.
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
HtmlGenericControl ctl = (HtmlGenericControl)e.Item.FindControl("lg");
ctl.InnerText //This is what will give you the result.
}
}
The legend tag that you use is not visible on server side. Is a client control and not a server one.
Try with this:
<legend id="myLegend" runat="server"><%# Container.DataItem %></legend>
Then in codebehind:
protected void ItemBound(Object sender, RepeaterItemEventArgs e)
{
if (e.Item.DataItem == null) return;
HtmlGenerics body = (HtmlGenerics)e.Item.FindControl("myLegend");
body.InnerText = "Foo";
}
I have a repeater and each item contains a button which should redirect to another page and also pass a value in the query string.
I am not getting any errors, but when I click the button, the page just refreshes (so I am assuming a postback does occur) and does not redirect. I think that for some reason, it isn't recognizing the CommandName of the button.
Repeater code:
<asp:Repeater ID="MySponsoredChildrenList" runat="server" OnItemDataBound="MySponsoredChildrenList_ItemDataBound" OnItemCommand="MySponsoredChildrenList_ItemCommand">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<br />
<div id="OuterDiv">
<div id="InnerLeft">
<asp:Image ID="ProfilePic" runat="server" ImageUrl='<%#"~/Resources/Children Images/" + String.Format("{0}", Eval("Primary_Image")) %>'
Width='300px' Style="max-height: 500px" /></div>
<div id="InnerRight">
<asp:HiddenField ID="ChildID" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "Child_ID") %>'/>
<span style="font-size: 20px; font-weight:bold;"><%# DataBinder.Eval(Container.DataItem, "Name") %>
<%# DataBinder.Eval(Container.DataItem, "Surname") %></span>
<br /><br /><br />
What have you been up to?
<br /><br />
<span style="font-style:italic">"<%# DataBinder.Eval(Container.DataItem, "MostRecentUpdate")%>"</span>
<span style="font-weight:bold"> -<%# DataBinder.Eval(Container.DataItem, "Update_Date", "{0:dd/MM/yyyy}")%></span><br /><br /><br />Sponsored till:
<%# DataBinder.Eval(Container.DataItem, "End_Date", "{0:dd/MM/yyyy}")%>
<br /><br />
<asp:Button ID="ChildProfileButton" runat="server" Text="View Profile" CommandName="ViewProfile" />
</div>
</div>
<br />
</ItemTemplate>
<SeparatorTemplate>
<div id="SeparatorDiv">
</div>
</SeparatorTemplate>
</asp:Repeater>
C# Code behind:
protected void MySponsoredChildrenList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
// Stuff to databind
Button myButton = (Button)e.Item.FindControl("ChildProfileButton");
myButton.CommandName = "ViewProfile"; }
}
protected void MySponsoredChildrenList_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "ViewProfile")
{
int ChildIDQuery = Convert.ToInt32(e.Item.FindControl("ChildID"));
Response.Redirect("~/ChildDescription.aspx?ID=" + ChildIDQuery);
}
}
I am new to using repeaters so it's probably just a rookie mistake. On a side note: is there a better way of obtaining the ChildID without using a hidden field?
EDIT: Using breakpoints; the ItemDatabound event handler is being hit, but the ItemCommand is not being entered at all
You need to set MySponsoredChildrenList_ItemDataBound as protected. Right now, you have just 'void' which by default is private, and is not accessible to the front aspx page.
Another way is to use the add event handler syntax from a function in your code behind, using the += operator.
Either way, the breakpoint will now be hit and our code should mwork.
EDIT: So the above solved the compilation error but the breakpoints are not being hit; I've ran some tests and am able to hit breakpoints like this:
Since I do not know how you are databinding, I just added this code to my code-behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
MySponsoredChildrenList.DataSource = new List<object>() { null };
MySponsoredChildrenList.DataBind();
}
}
Note: If you DataBind() and ItemDataBound() is called on every postback, it will wipe out the command argument, which is potentially what you are seeeing; so if you always see [object]_ItemDataBound() breakpoint hit, but never [object]_ItemCommand(), it is because you need to databind only on the initial page load, or after any major changes are made.
Note also the method MySponsoredChildrenList_ItemCommand doesn't work:
protected void MySponsoredChildrenList_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "ViewProfile")
{
int ChildIDQuery = Convert.ToInt32(e.Item.FindControl("ChildID"));
Response.Redirect("~/ChildDescription.aspx?ID=" + ChildIDQuery);
}
}
When you do FindControl, you need to cast to the correct control type, and also make sure to check for empty and null values before converting, or else you will possibly have errors:
protected void MySponsoredChildrenList_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "ViewProfile")
{
int childId = 0;
var hiddenField = e.Item.FindControl("ChildID") as HiddenField;
if (null != hiddenField)
{
if (!string.IsNullOrEmpty(hiddenField.Value))
{
childId = Convert.ToInt32(hiddenField.Value);
}
}
if (childId > 0)
{
Response.Redirect("~/ChildDescription.aspx?ID=" + childId);
}
}
}
Hope this helps - if not, please post additional code for the "full picture" of what is happening, so we can see where else you might have a problem.
I think on the repeater you need to add
OnItemDataBound="MySponsoredChildrenList_ItemDataBound"
Not 100% sure, but could be the same for the ItemCommand.
--
In regards to obtaining the ChildID. Add a CommandArgument to the button in the repeater, and set it in the same way, <% Eval("ChildID") %>. This can the be obtained using e.CommandArgument.
Try this.. Add an attribute to item row
row.Attributes["onclick"] = string.Format("window.location = '{0}';", ResolveClientUrl(string.Format("~/YourPage.aspx?userId={0}", e.Item.DataItem)) );
or You can do like this also
<ItemTemplate> <tr onmouseover="window.document.title = '<%# Eval("userId", "Click to navigate onto user {0} details page.") %>'" onclick='window.location = "<%# ResolveClientUrl( "~/YourPage.aspx?userId=" + Eval("userid") ) %>"'> <td> <%# Eval("UserId") %> </td> </tr> </ItemTemplate>
I am using ASP.NET repeater and I want to display No Row Found message when query return 0 rows from database. I know its there in GridView.
Regards
If you have a HeaderTemplate or a FooterTemplate defined, you could add any HtmlControl or ServerControl inside of either of them and then programatically show/hide it in the codebehind.
<asp:Repeater id="Repeater1" runat="server" OnItemDataBound="">
<HeaderTemplate>
<h1>My Repeater Data</h1>
<div id="NoRecords" runat="server" visible="false">
No records are available.
</div>
</HeaderTemplate>
<ItemTemplate>
...
</ItemTemplate>
</asp:Repeater>
Here's the code behind
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (Repeater1.Items.Count < 1)
{
if (e.Item.ItemType == ListItemType.Header)
{
HtmlGenericControl noRecordsDiv = (e.Item.FindControl("NoRecords") as HtmlGenericControl);
if (noRecordsDiv != null) {
noRecordsDiv.Visible = true;
}
}
}
}
Think about using the ListView control instead that has an EmptyDataTemplate for use when the data source has no data. If you opt to stick with the Repeater control, think about testing your query for records and optionally displaying a Label or Literal that has your "no row found" message instead of your repeater.
if (query.Any())
{
repeater.DataSource = query;
repeater.DataBind();
}
else
{
noRecordsLiteral.Visible = true;
}
Here is a simple example to demonstrate Displaying No Record Found in a Repeater control.
<div id="NoRecords" runat="server" visible="false">
No records are available.
</div>
<asp:Repeater id="Repeater1" runat="server" OnPreRender="Repeater1_PreRender">
<HeaderTemplate>
<h1>My Repeater Data</h1>
</HeaderTemplate>
<ItemTemplate>
...
</ItemTemplate>
</asp:Repeater>
Here is the Code-Behind
protected void Repeater1_PreRender(object sender, EventArgs e)
{
if (Repeater1.Items.Count < 1)
{
NoRecords.Visible = true;
Repeater1.Visible = false;
}
}
You could do this by altering the logic in the repeater or by providing data to the repeater that drives the behavior you want. I prefer that the repeater is left out of it. It's a keep your logic out of the view thing if you are familiar with MVC.
I am saving space here by using a List as the data source instead of a database result but the principle is the same. You would probably have a collection of IDataRecord as your source if you are returning from a DB.
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Linq;
namespace StackOverflowRepeater
{
public partial class Default : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
repeater.ItemDataBound += HandleRepeaterItemDataBound;
var data = new List<string>();
if (!data.Any()) // could also be data.Count < 1
{
data.Add("No Row Found");
}
repeater.DataSource = data;
repeater.DataBind();
base.OnInit(e);
}
void HandleRepeaterItemDataBound (object sender, RepeaterItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.AlternatingItem) || (e.Item.ItemType == ListItemType.Item))
{
var span = (HtmlGenericControl) e.Item.FindControl("output");
span.InnerText = e.Item.DataItem.ToString();
}
}
}
}
This is assuming the following mark-up:
<%# Page Language="C#" Inherits="StackOverflowRepeater.Default" %>
<html>
<body>
<form runat="server">
<asp:Repeater id='repeater' runat="server">
<ItemTemplate>
<span id='output' runat="server" />
</ItemTemplate>
</asp:Repeater>
</form>
</body>
</html>
I done something following;
<asp:Repeater ID="Repeater" runat="server">
<HeaderTemplate>
<table>
<% if (Repeater.Items.Count < 1)
{ %>
<tr>
<td colspan="3">No data found.</td>
</tr>
<% }
else { %>
<tr>
<th>Case Id</th>
<th>Claim Id</th>
<th>Notes</th>
</tr>
<% } %>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td rowspan="2"><%# Eval("CaseId") %></td>
<td rowspan="2">
<%# Eval("ClaimId") %>
</td>
<td><%# Eval("Note") %></td>
</tr>
<tr>
<td><%# Eval("CreatedBy") %> : <%# Eval("CreatedDate") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
You can also do it like this:
CSS
.div-no-display {
display: none;
}
Code Behind - Usually don't bind it if its empty in that case you have to put the div outside the repeater, but if you bind 0 items you can put it in the header template
this.Repeater.DataSource = ChildLinksList;
this.Repeater.DataBind();
public List<Link> ChildLinksList { get; set; }
Page
<HeaderTemplate>
<div id="NoRecords" class='<%= ChildLinksList.Count > 0 ? "div-no-display" : "" %>'>
No child links active.
</div>
</HeaderTemplate>
I have a nested repeater control that displays a list of data, in my case it is an FAQ list. here is the design portion:
<asp:Repeater ID="lists" runat="server">
<ItemTemplate>
<h2 class="sf_listTitle"><asp:Literal ID="listTitle" runat="server"></asp:Literal></h2>
<p class="sf_controlListItems">
<a id="expandAll" runat="server">
<asp:Literal ID="Literal1" runat="server" Text="<%$Resources:ExpandAll %>"></asp:Literal>
</a>
<a id="collapseAll" runat="server" style="display:none;">
<asp:Literal ID="Literal2" runat="server" Text="<%$Resources:CollapseAll %>"></asp:Literal>
</a>
</p>
<ul class="sf_expandableList" id="expandableList" runat="server">
<asp:Repeater ID="listItems" runat="server">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<li>
<h1 id="headlineContainer" runat="server" class="sf_listItemTitle">
<a id="headline" runat="server" title="<%$Resources:ClickToExpand %>"></a>
</h1>
<div id="contentContainer" runat="server" class="sf_listItemBody" style="display:none;">
<asp:Literal ID="content" runat="server"></asp:Literal>
</div>
</li>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
</ul>
</ItemTemplate>
</asp:Repeater>
The repeater that I am interested in is the second repeater, listItems. In my code-behind, I cannot directly call listItems and see the controls inside of it. I tried to grab the control inside of list.DataBinding (maybe I need to use a different event?) method:
void lists_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
var oRepeater = (Repeater) lists.FindControl("listItems");
}
but this comes up as null. Can anyone give me some pointers/tips of what I need to do to gain access to the listItems repeater and it's children controls?
Thanks!
lists
belongs to each RepeaterItem, not directly to the Repeater itself.
Try :-
void lists_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if ( e.Item.ItemType == ListItemType.AlternatingItem
|| e.Item.ItemType == ListItemType.Item )
{
Repeater oRepeater = (Repeater)e.Item.FindControl("listItems");
// And to get the stuff inside.
foreach ( RepeaterItem myItem in oRepeater.Items )
{
if ( myItem.Item.ItemType == ListItemType.AlternatingItem
|| myItem.Item.ItemType == ListItemType.Item )
{
Literal myContent = (Literal)myItem.FindControl("content");
// Do Something Good!
myContent.Text = "Huzzah!";
}
}
}
}
And you should be good :)
Edited to incorporate DavidP's helpful refinement.
You need to change that line to
var oRepeater = (Repeater) e.Item.FindControl("listItems");
You're close! Inside your event handler check the RepeaterItemEventArgs for what kind of row you're dealing with. Your child repeater will only be available on (Alt)Item rows, not headers or footers. My guess is that it's blowing up on the header.