Even though ASP.NET Repeater control does not have sorting option as the Gridview does, it can be manually coded easily using the Dataview. To implement the sorting include in
.aspx page
<asp:Repeater ID="Repeater" runat="server" ClientIDMode="Static" OnItemCommand="Repeater_ItemCommand">
<HeaderTemplate>
<table cellspacing="2" cellpadding="5" >
<tr style="background-color: #D2B48C;">
<th align="center" style="width: 20px;">
</th>
<th align="center" style="width: 55px;">
<asp:LinkButton ID="ClaimNoLinkButton" runat="server" Text="ClaimNo" CommandName="ClaimNo"></asp:LinkButton>
</th>
<th align="left" style="width: 100px;">
<asp:LinkButton ID="LastUpdatedOnLinkButton" runat="server" Text="LastUpdatedOn"
CommandName="LastUpdatedOn"></asp:LinkButton>
</th>
</tr>
</HeaderTemplate>
<FooterTemplate>
<tr>
{ contents }
</tr>
</table>
</FooterTemplate>
<ItemTemplate>
<tr>
{ contents }
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr>
{ contents }
</tr>
</AlternatingItemTemplate>
</asp:Repeater>
.cs page
In page load, if the page is not postback,
protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.IsPostBack)
{
//Set View State Value for Initial Sort Order
ViewState["Column"] = "ClaimNo";
ViewState["Order"] = "DESC";
BindRepeater();
}
}
protected void Repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == ViewState["Column"].ToString())
{
if (ViewState["Order"].ToString() == "ASC")
ViewState["Order"] = "DESC";
else
ViewState["Order"] = "ASC";
}
else
{
ViewState["Column"] = e.CommandName;
ViewState["Order"] = "ASC";
}
}
public void BindRepeater()
{
DataTable dt = new DataTable();
dt.Columns.Add("ClaimNo", typeof(int));
dt.Columns.Add("LastUpdatedOn", typeof(DateTime));
dt.Rows.Add(1, "2012-03-16 00:00:00.000");
dt.Rows.Add(2, "2012-02-16 00:00:00.000");
Repeater.DataSource = dt;
Repeater.DataBind();
DataView dvSort = new DataView(dt);
dvSort.Sort = ViewState["Column"].ToString() + " " + ViewState["Order"].ToString(); //Sorting columns in Repeater
Repeater.DataSource = dvSort;
Repeater.DataBind();
}
.aspx page
<asp:Repeater ID="Repeater" runat="server" ClientIDMode="Static" OnItemCommand="Repeater_ItemCommand">
<HeaderTemplate>
<table cellspacing="2" cellpadding="5" >
<tr style="background-color: #D2B48C;">
<th align="center" style="width: 20px;">
</th>
<th align="center" style="width: 55px;">
<asp:LinkButton ID="ClaimNoLinkButton" runat="server" Text="ClaimNo" CommandName="ClaimNo"></asp:LinkButton>
</th>
<th align="left" style="width: 100px;">
<asp:LinkButton ID="LastUpdatedOnLinkButton" runat="server" Text="LastUpdatedOn"
CommandName="LastUpdatedOn"></asp:LinkButton>
</th>
</tr>
</HeaderTemplate>
<FooterTemplate>
<tr>
{ contents }
</tr>
</table>
</FooterTemplate>
<ItemTemplate>
<tr>
{ contents }
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr>
{ contents }
</tr>
</AlternatingItemTemplate>
</asp:Repeater>
.cs page
In page load, if the page is not postback,
protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.IsPostBack)
{
//Set View State Value for Initial Sort Order
ViewState["Column"] = "ClaimNo";
ViewState["Order"] = "DESC";
BindRepeater();
}
}
protected void Repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == ViewState["Column"].ToString())
{
if (ViewState["Order"].ToString() == "ASC")
ViewState["Order"] = "DESC";
else
ViewState["Order"] = "ASC";
}
else
{
ViewState["Column"] = e.CommandName;
ViewState["Order"] = "ASC";
}
}
public void BindRepeater()
{
DataTable dt = new DataTable();
dt.Columns.Add("ClaimNo", typeof(int));
dt.Columns.Add("LastUpdatedOn", typeof(DateTime));
dt.Rows.Add(1, "2012-03-16 00:00:00.000");
dt.Rows.Add(2, "2012-02-16 00:00:00.000");
Repeater.DataSource = dt;
Repeater.DataBind();
DataView dvSort = new DataView(dt);
dvSort.Sort = ViewState["Column"].ToString() + " " + ViewState["Order"].ToString(); //Sorting columns in Repeater
Repeater.DataSource = dvSort;
Repeater.DataBind();
}
awesome .. i was looking for this.. simply and easy ..
ReplyDeletethank u so much
u saved my day dear thanks alot
ReplyDeletedt.Rows.Add(1, "2012-03-16 00:00:00.000");
ReplyDeletedt.Rows.Add(2, "2012-02-16 00:00:00.000");
Can you explain about these tow lines?
I got bug when i working out..please its urgent.
In this i have to bind data from Database..
ReplyDeleteHow to proceed?