Repeater PageCount and PageIndex - asp.net

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.

Related

UserControl not loading in ListView

I've create my own UserControl called 'ReevooBadge' and have placed it in my listview by registering my UserControl. However, my UserControl isn't visible in the ListView. Any sugestions for this problem?
This is my User Control
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="ReevooBadge.ascx.cs" Inherits="KiaEurope2010.Website.controls.Addons.Widgets.ReevooBadge" %>
<a class="reevoomark <%= this.ClassName %>" href="//mark.reevoo.com/partner/<%= this.CountryCode %>/series:<%= this.SelectionIdentifier %>"><%= this.ReviewsTitle %></a>
I need to implement this in my ListView by the following code implementation
<asp:ListView ID="lvItemCarousel" ItemPlaceholderID="carrouselitems"OnItemDataBound="lvItemCarousel_ItemDataBound"
runat="server">
<layouttemplate>
<asp:PlaceHolder ID="carrouselitems" runat="server" />
</layouttemplate>
<itemtemplate>
<li>
<asp:HyperLink runat="server" ID="hlItem">
<asp:Image runat="server" ID="imItem" />
<div class="modelTitle">
<asp:PlaceHolder runat="server" ID="phReevoo" Visible="false">
<rhm:ReevooBadge runat="server" ID="ReevooBadge" />
</asp:PlaceHolder>
</div>
</asp:HyperLink>
</li>
</itemtemplate>
</asp:ListView>
Above I register my custom User Control
<%# Register Src="~/controls/Addons/Widgets/ReevooBadge.ascx" TagPrefix="rhm" TagName="ReevooBadge" %>
And in the Item Data Binding
protected void lvItemCarousel_ItemDataBound(object sender, ListViewItemEventArgs e)
{
//...
phReevoo.Visible = true;
ReevooBadge.ModelItem = this.Item;
ReevooBadge.CountryCode = KiaEurope.SitecoreLayer.Reevoo.Settings.GetReevooKey(LanguageCountryCultureHelper.CurrentCountryCode, Sitecore.Context.Language);
}
However, the user control isn't visible in the ListView.
I'm not sure how you're getting your code to compile because I couldn't reference phReevoo (the placeholder control) directly in the ItemDataBound event.
I needed to use the following:
e.Item.FindControl("phReevoo").Visible = true;
The same would apply if you tried to reference ReevooBadge.
To make the testing easier I created a web user control and used this as a substitute for your custom user control. After binding to a simple List<string>, the control showed without issue.
I would suggest checking if your user control is actually rendering outside of the ListView.

How to loop through data in WebForms like in MVC

How do I loop through data in WebForms like I do in ASP.NET MVC? For instance, in MVC, this is as simple as:
<table>
#foreach (var myItem in g)
{
#<tr><td>#MyItem.title<td></tr>
}
</table>
What would the code behind look like?
Or, can I add an MVC project to a WebForms application so that I can use MVC functionality, instead?
Rather than use a repeater, you can just loop through the list in a similar MVC type way using the <% %> and <%= %> tags.
<table>
<% foreach (var myItem in g) { %>
<tr><td><%= myItem.title %></td></tr>
<% } %>
</table>
As long as the property you're looping through is acessible from the aspx/ascx page (e.g. declared as protected or public) you can loop through it. There is no other code in the code behind necessary.
<% %> will evaluate the code and <%= %> will output the result.
Here is the most basic example:
Declare this list at your class level in your code behind:
public List<string> Sites = new List<string> { "StackOverflow", "Super User", "Meta SO" };
That's just a simple list of strings, so then in your aspx file
<% foreach (var site in Sites) { %> <!-- loop through the list -->
<div>
<%= site %> <!-- write out the name of the site -->
</div>
<% } %> <!--End the for loop -->
In WebForm you can use Repeater control:
<asp:Repeater id="cdcatalog" runat="server">
<ItemTemplate>
<td><%# Eval("title")%></td>
</ItemTemplate>
</asp:Repeater>
In code behind:
cdcatalog.DataSource = yourData;
cdcatalog.DataBind();
You can use a Repeater with any sort of valid DataSource (SqlDataSource, EntityDataSource, ObjectDataSource) object:
Define the DataSource
Reference the DataSource in your Reperater
....
<asp:Repeater id="someRep" runat="server" DataSourceID="YourDataSource">
<ItemTemplate>
<tr>
<td><%# Eval("PropertyName") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
...

How to use Eval in codebehind to set Page.Title

I have a SQLDataSource that is bound to a ListView control but I want to place parts of the bound record into the HTML TITLE attribute. Here is my codebehind file that I want to change so it can use Eval to construct a dynamic TITLE based on the data content:
Public Partial Class zShowAd
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Page.Title = " Dynamically set in ASPX page"
'how to use Eval here instead of the above constant ??
End Sub
End Class
Here is the corresponding .aspx file:
<%# Page Language="vb" AutoEventWireup="false" MasterPageFile="~/zSEO.master"
CodeBehind="zShowAd.aspx.vb" Inherits="Zipeee.zShowAd" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div>
<asp:ListView ID="ShowAd" runat="server" DataSourceID="aPosting">
<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
</LayoutTemplate>
<ItemTemplate>
<div>
<div id="wrapper">
<div id="header"></div>
<div id="main">
<div id="nav"> AdID: <%#Eval("AdID")%></div>
<div id="extras">Price: <%#Eval("Price")%></div>
<div id="content"> <%#Eval("AdDesc")%></div>
</div>
<div id="footer"></div>
</div>
</div>
</ItemTemplate>
</asp:ListView>
<asp:sqldatasource runat="server" id="aPosting"
ConnectionString="<%$ ConnectionStrings:ZIPeeeConnectionString2 %>"
SelectCommand="spGetAdByID" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:QueryStringParameter Name="AdId" QueryStringField="a" Type="String" />
</SelectParameters>
</asp:sqldatasource>
</div>
</asp:Content>
You can call a method (Sub) of the page's code behind by putting the following somewhere inside the ItemTemplate of your ListView:
<%# SetPageTitle(Eval("SomeProperty")) %>
Then in your code behind (sorry it's in C#):
protected void SetPageTitle(object title)
{
this.Title = title.ToString();
}
Alternatively, you can also pass the complete data item, instead of just one property:
<%# SetPageTitle(Container.DataItem) %>
Update (to answer your comment):
<%# ... %> is a so-called data-binding expression. It only works inside of a data-bound control (the ListView in your example) and it always works with the current record (typically you display more than one record in a data-bound control like the ListView).
So when you use <%# Eval("Price") %>, you are displaying the value of the current record's "Price" column. If your query, would return more than one record, then this would be executed for each record, and when setting the page title (as shown above), the page's title would be the value from the last record.
On the other hand <%= ... %>, is just a normal server-side code snippet (don't know if there is a specific name for it), which does not know about the data-binding context (e.g. which is the current record).
Please see the following question for more details: When should I use # and = in ASP.NET controls?

VB.NET Repeater Simple Data Binding Without Datasource

Im a ASP.NET beginner. I previously asked how to do some databinding to a repeater without a datasourse. Here. VB.NET Repeater Simple Data Binding Without Datasource
here is the solution someone got for me
Dim repeatTimes((TotalAdInsured)) As Integer
myRepeater.DataSource = repeatTimes
myRepeater.DataBind()
However, I now need to know how to get an index for each item in the repeater. ie
<% #index %>
<asp:Repeater runat="server" ID="rptAwesome">
<ItemTemplate>
<%# Container.DataItem %> <%# Container.ItemIndex %><br />
</ItemTemplate>
</asp:Repeater>

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