我有一个 ListView - 从数据表绑定 - 包含几页按时间顺序排列的项目。
我希望能够按升序和降序对它们进行排序。
在我的<layouttemplate>
我有以下内容:
<asp:linkbutton runat="server" id="SortbyYear" commandname="Sort" commandargument="Year">Year</asp:linkbutton>
当我单击它时,我从 pageRequestManager 收到一个错误,即未处理排序。
所以我将以下内容添加到我的<asp:listview ... >
:
onsorting="HistoryList_Sorting"
我实际上在后面的代码中放入了该方法以使排序正常工作?我只是尝试在升序和降序之间来回切换“年”列。
protected void HistoryList_Sorting(object sender, ListviewSortEventArgs e)
{
// WHAT GOES HERE???
}
更新:
以下是我的 ASPX 页面中的内容:
<asp:listview id="HistoryList" runat="server" convertemptystringtonull="False" onlayoutcreated="HistoryList_LayoutCreated" ondatabound="HistoryList_DataBound" onsorting="HistoryList_Sorting" >
<layouttemplate>
<table>
<tr>
<th><asp:linkbutton runat="server" id="SortByYear" commandname="Sort" commandargument="Year"><asp:literal runat="server" id="Year" /></asp:linkbutton></th>
<th><asp:literal runat="server" id="Event" /></th>
</tr>
<tr id="ItemPlaceholder" runat="server"></tr>
</table>
</layouttemplate>
<itemtemplate>
<tr class="row">
<td class="history-year-column"><%# Eval("Year") %></td>
<td><%# Eval("Description") %></td>
</tr>
</itemtemplate>
<alternatingitemtemplate>
<tr class="row-alternate">
<td class="history-year-column"><%# Eval("Year") %></td>
<td><%# Eval("Description") %></td>
</tr>
</alternatingitemtemplate>
</asp:listview>
以下是我的 CS 页面中的内容:
#region " Declare: Shared Classes "
private Localization localizeSite = new Localization();
private DataXML xmlData = new DataXML();
public DataTable HistoryDataTable { get; set; }
#endregion
#region " Page: PreInit "
private void Page_PreInit(object sender, System.EventArgs e)
{
Page.MasterPageFile = localizeSite.LoadMasterPage(Page.Master.AppRelativeVirtualPath);
}
#endregion
#region " Page: Load "
protected void Page_Load(object sender, System.EventArgs e)
{
Edition edition = new Edition();
ContentTracking.Text = edition.GetEdition(Page.AppRelativeVirtualPath);
//// LOCALIZE THE WEB SITE CONTENT
Heading.Text = localizeSite.LocalizeText(Page, "Heading.Text");
Body.Text = localizeSite.LocalizeText(Page, "Body.Text");
AdNetworkTracking.Text = localizeSite.LocalizeText(Page, "AdNetworkTracking.Text");
}
#endregion
#region " Handle: OnLayoutCreated "
protected void HistoryList_LayoutCreated(object sender, System.EventArgs e)
{
((Literal)HistoryList.FindControl("Year")).Text = localizeSite.LocalizeText(Page, "Year.Text");
((Literal)HistoryList.FindControl("Event")).Text = localizeSite.LocalizeText(Page, "Event.Text");
}
#endregion
#region " Handle: Sorting "
public String SortExpression
{
get
{
return (string)ViewState["SortExpression"];
}
set
{
ViewState["SortExpression"] = value;
}
}
protected void HistoryList_Sorting(object sender, ListViewSortEventArgs e)
{
String sortExpression = e.SortExpression + " " + e.SortDirection.ToString();
this.SortExpression = sortExpression.Replace("Ascending", "ASC").Replace("Descending", "DESC");
}
#endregion
#region " Handle: Paging "
protected void HistoryList_DataBound(object sender, EventArgs e)
{
HistoryPager.Visible = (HistoryPager.PageSize < HistoryPager.TotalRowCount);
}
protected void HistoryPager_PreRender(object sender, EventArgs e)
{
if (HistoryDataTable == null)
{
HistoryDataTable = xmlData.GetDataTable(Server.MapPath("~/App_Data/history.xml"), "Event");
}
if (!String.IsNullOrEmpty(SortExpression))
{
HistoryDataTable.DefaultView.Sort = SortExpression;
}
HistoryList.DataSource = HistoryDataTable;
HistoryList.DataBind();
}
#endregion
排序还没有工作。