我在我的 ASP.Net 应用程序中使用 ListView 与列
[Lastname] [Firstname] [telephonenumber][Department][eMail]
和
我的 ListView 内置了 LayoutTemplate、ItemTemplate 和 AlternatingItemTemplate。
现在我希望如果我单击The Headertext Lastname or Firstname ,我会得到一个排序列表。我怎么能做到这一点?
这是我的列表视图:
<asp:ListView runat="server" ID="myListView" OnItemCommand="myListView_ItemCommand" OnSelectedIndexChanging="myListView_SelectedIndexChanging">
<LayoutTemplate>
<table id="UserTable" runat="server" border="0" width="800" cellpadding="0" cellspacing="0">
<tr style="background-color:#E5E5FE" class="TableClass">
<th align="left" id="th4" runat="server"><asp:LinkButton CommandName="Sort" CommandArgument="Nachname" ID="lnkNachname" runat="server" >id_Nachname</asp:LinkButton></th>
<th align="left" id="th3" runat="server"><asp:LinkButton CommandName="Sort" CommandArgument="Vorname" ID="lnkVorname" runat="server" >id_Vorname</asp:LinkButton></th>
<th align="left" id="th5" runat="server"><asp:Label ID="lnkTelefon" runat="server" >id_Telefon</asp:Label></th>
<th align="left" id="th6" runat="server"><asp:Label ID="lnkAbteilung" runat="server">id_Abteilung</asp:Label></th>
<th align="left" id="th2" runat="server"><asp:Label ID="lnkemail" runat="server" >id_Email</asp:Label></th>
</tr>
<tr runat="server" id="ItemPlaceholder">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr class="TableClass">
<td align="left"><asp:LinkButton CssClass="MyLink" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label4" Text='<%# Eval("Nachname") %>' runat="server" /></td>
<td align="left"><asp:LinkButton CssClass="MyLink" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label3" Text='<%# Eval("Vorname") %>' runat="server" /></td>
<td align="left"><asp:LinkButton CssClass="MyLink" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label5" Text='<%# Eval("Telefonnummer") %>' runat="server" /></td>
<td align="left"><asp:LinkButton CssClass="MyLink" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label7" Text='<%# Eval("Abteilung") %>' runat="server" /></td>
<td align="left"><asp:LinkButton CssClass="MyLink" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label2" Text='<%# Eval("eMail") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label6" Text='<%# Eval("GUID") %>' runat="server" Visible="False" /></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr class="TableClass">
<td align="left"><asp:LinkButton CssClass="MyLink" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label4" Text='<%# Eval("Nachname") %>' runat="server" /></td>
<td align="left"><asp:LinkButton CssClass="MyLink" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label3" Text='<%# Eval("Vorname") %>' runat="server" /></td>
<td align="left"><asp:LinkButton CssClass="MyLink" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label5" Text='<%# Eval("Telefonnummer") %>' runat="server" /></td>
<td align="left"><asp:LinkButton CssClass="MyLink" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label7" Text='<%# Eval("Abteilung") %>' runat="server" /></td>
<td align="left"><asp:LinkButton CssClass="MyLink" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label2" Text='<%# Eval("eMail") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label6" Text='<%# Eval("GUID") %>' runat="server" Visible="False" /></td>
</tr>
</AlternatingItemTemplate>
</asp:ListView>
cs代码:
protected void myListView_Sorting(object sender, ListViewSortEventArgs e)
{
if (e.SortExpression == "Vorname")
{
e.SortDirection = System.Web.UI.WebControls.SortDirection.Ascending;
}
else
{
e.SortDirection = System.Web.UI.WebControls.SortDirection.Ascending;
}
}