Repeater control: How to access dataitem property in itemtemplate - asp.net

In Repeater control I can access the dataitem property in item template using <%# Eval("IsAvailable") %>,I want to apply if condition on IsAvailable and toggle showing div using <% %> syntax,how do I achieve that?
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<% if([IsAvailable]){ %>
<div>test1</div>
<% }else{ %>
<div>test2</div>
<% }%>
</ItemTemplate>
</asp:Repeater>

Related

ASP.Net repeater control - using conditional statements

I am trying to use an if statement inside of a repeater control and receiving an InvalidOperationException on the if.
What I'm trying to do is run a block of code inside the repeater only if the current item has a UserType that is set to Admin.
<asp:Repeater ID="rptSingleValueHeaders" runat="server">
<ItemTemplate>
<% if (Eval("UserType").ToString() == "Admin") { %>
<div>
do stuff here
</div>
<% } else { %>
<div>
do other stuff
</div>
<% } %>
</ItemTemplate>
</asp:Repeater>
My datasource is defined on the aspx.cs and contains a property named UserType which is of type string. Let me know if I need to provide any more details. Thank you.
You could use server side visibility:
<ItemTemplate>
<div runat="server" visible='<%# (Eval("UserType").ToString() == "Admin") %>'>
I show this HTML
</div>
<div runat="server" visible='<%# (Eval("UserType").ToString() != "Admin") %>'>
I show this other HTML
</div>
</ItemTemplate>

How to get the value inside a repeaters itemtemplate?

<asp:Repeater ..>
<ItemTemplate>
<% string age = Eval("a").ToString() %>
<%
age = a.ToLower(); // real stuff here
%>
<p>Hello <%# Eval("name") %> you are <%= age %> old</p>
</ItemTemplate>
</asp:Repeater>
I'm getting an error saying:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Use <%# Eval("<propertyName>") %>
Of course you will have to assign a DataSource to your repeater, and call DataBind()
And, without using those inline coding, you can wrap the whole logic to a custom property for your data item. For example, as in the above code, you can create a custom property say, Age like:
partial class YourDataItemClass // use partial if it is auto-generated
{
public string Age
{
var ageStr = a.ToString(); // assuming YourDataItemClass has an `a` var/property
// Do real stuff here
...
...
var lowered = ageStr.ToLower();
...
...
return lowered;
}
}
and you can expose that property inside the repeater control like:
<asp:Repeater id="myRepeater" ..>
<ItemTemplate>
<p>Hello <%# Eval("Name") %> you are <%# Eval("Age") %> old</p>
</ItemTemplate>
</asp:Repeater>
Assign datasource and databind the repeater somewhere in the code-behind like:
...
// Call the method which provides you the data
// IEnumerable<YourDataItemClass> myData = ... ;
myRepeater.DataSource = myData;
myRepeater.DataBind();
...
<asp:Repeater>
<ItemTemplate>
<p>Hello <%# Eval("name") %> you are <%# Convert.ToString(Eval("a")).ToLower() %> old</p>
</ItemTemplate>
</asp:Repeater>

Repeater PageCount and PageIndex

is there pageindex or pagecount for repeater? i able to do with gridview and could not find much with repeater and not in code-behind too, how would i do PageIndex and PageCount in Repeater?
Page <%# rpt.PageIndex + 1 %> of <%# rpt.PageCount %>
<asp:Repeater ID="rpt" runat="server" OnItemCommand="rpt_OnItemCommand" OnItemDataBound="rpt_OnItemDataBound">
<HeaderTemplate>
<div >
<div >
Page <%# rpt.PageIndex + 1 %> of <%# rpt.PageCount %>
</div>
</div>
</HeaderTemplate>
<ItemTemplate ......
</asp:Repeater>
Repeater control does not support paging out of the box, which means you have to implement it yourself. One way (and the easiest probably) is to use PagedDataSource object, which encapsulates paging-related properties and logic. There is a couple of examples in web on how to accomplish this: example one, example two, example three.

Insert a User Control within an ASP:Repeater

I need to insert a user control into a repeater
<asp:Repeater runat="server" ID="rptAdditionalPages">
<ItemTemplate>
<div id="<%# ((ftj.com.AdditionalPageForProductDetail)Container.DataItem).DivID %>" class="tab_content">
<h1><%# ((ftj.com.AdditionalPageForProductDetail)Container.DataItem).Title %></h1>
<%# ((ftj.com.AdditionalPageForProductDetail)Container.DataItem).Body %>
<uc:EnrollmentMethod ID="EnrollmentMethod2" runat="server" />
</div>
</ItemTemplate>
</asp:Repeater>
When the user control is inserted with this method the code behind cannot find EnrollmentMethod2. Is it possible to add user controls in repeaters?
You can't find it because it's nested in the repeater. You will need to dig into the repeater to find it.
foreach (RepeaterItem item in Repeater1.Items)
{
EnrollmentMethod control = (EnrollmentMethod )item.FindControl("EnrollmentMethod2");
}

ASP.NET: How to convert <A> or HtmlAnchor to static text?

i have a repeater that will output a series of items:
<asp:repeater ... runat="Server">
<itemtemplate>
<%# GetItemText %>
<itemtemplate>
<asp:repeater>
But some items will not have an associated link, so i don't want them to be clickable. i tried making it a runat=server HtmlAnchor, and set the htmlAnchor.Disabled = true for the items are should not actually have a link - but they can still be clicked (it just makes the text gray)
i know how i'd do it in the olden days:
<% If IsLink Then %>
<A href="<% =GetItemLink%">
<% End If %>
<% =GetItemText %>
<% If IsLink Then %>
</A>
<% End If %>
But that's the messy mixing code and html ASP way. What's the ASP.NET way?
Use an <asp:HyperLink > control, which displays the text normally if no link is supplied.
Edited to include example:
<asp:repeater ... runat="Server">
<itemtemplate>
<asp:HyperLink ... runat="server" NavigateUrl="<%# GetItemLink(...) %>"> <%# GetItemText %></asp:HyperLink>
<itemtemplate>
<asp:repeater>
In the above example, the anchor tag will be rendered to html regardless, but if the NavigateUrl attribute is an empty string, there will be no href at all and every browser I've ever used renders the text in a manner similar to spans (so look out for custom styles on <a >'s).

Resources