我在带有列的ASP.Net应用程序中使用ListView
[Lastname] [Firstname] [telephonenumber][Department][eMail]
和
我的ListView内置在LayoutTemplate,ItemTemplate和AlternatingItemTemplate。
现在我希望如果我在Headertext Lastname或Firstname上点击,我会得到一个排序列表。 我怎么能这样做?
这是我的ListView:
<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 code:
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;
}
}
答案 0 :(得分:1)
html代码是
<asp:ListView ID="lstLogs" runat="server" ItemPlaceholderID="tr">
<LayoutTemplate>
<table cellspacing="0">
<tr class="hdrRowColor1">
<td align="left" width="20px" style="display: none;">
<asp:CheckBox ID="selectall" runat="server" ClientIDMode="Static" /><%--onclick="toggleChecked(this.checked);"--%>
</td>
<td align="left" width="35px">
S.No
</td>
<td align="left" width="120px">
<asp:LinkButton ID="lnkName" runat="server" CommandArgument="FirstName" ValidationGroup="vgSearch"
OnClick="lnk_Sort">Name</asp:LinkButton>
<asp:Image ID="img_lnkName" Visible="false" runat="server" />
</td>
<td align="left" width="120px">
<asp:LinkButton ID="lnkEmail" runat="server" CommandArgument="u.EmailAddress" ValidationGroup="vgSearch"
OnClick="lnk_Sort">Email</asp:LinkButton>
<asp:Image ID="img_lnkEmail" Visible="false" runat="server" />
</td>
<td align="left" width="60px">
<asp:LinkButton ID="lnkRole" runat="server" CommandArgument="RoleName" ValidationGroup="vgSearch"
OnClick="lnk_Sort">Role</asp:LinkButton>
<asp:Image ID="img_lnkRole" Visible="false" runat="server" />
</td>
<td align="left" width="160px">
<asp:LinkButton ID="lnkPage" runat="server" CommandArgument="Page" ValidationGroup="vgSearch"
OnClick="lnk_Sort">Page Name</asp:LinkButton>
<asp:Image ID="img_lnkPage" Visible="false" runat="server" />
</td>
<td align="left" width="105px">
<asp:LinkButton ID="lnkMac" runat="server" CommandArgument="tblUserActivities.MacAddress"
ValidationGroup="vgSearch" OnClick="lnk_Sort">MAC Address</asp:LinkButton>
<asp:Image ID="img_lnkMac" Visible="false" runat="server" />
</td>
<td align="left" width="70px">
<asp:LinkButton ID="lnkIp" runat="server" CommandArgument="tblUserActivities.IpAddress"
ValidationGroup="vgSearch" OnClick="lnk_Sort">I.P Address</asp:LinkButton>
<asp:Image ID="img_lnkIp" Visible="false" runat="server" />
</td>
<td align="left" width="60px">
<asp:LinkButton ID="lnkCreatedDate" runat="server" CommandArgument="tblUserActivities.CreatedDate"
ValidationGroup="vgSearch" OnClick="lnk_Sort">Created Date</asp:LinkButton>
<asp:Image ID="img_lnkCreatedDate" Visible="false" runat="server" />
</td>
</tr>
<tr id="tr" runat="server">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr class='<%# Convert.ToBoolean(Container.DataItemIndex % 2) ? "EvenRowColor" : "OddRowColor" %>'>
<%--<td align="left">
<asp:CheckBox ID="chkItem" runat="server" CssClass="case" ClientIDMode="Static" />
<asp:Label ID="lblpKId" runat="server" Text='<%# Eval("pkActivityId") %>' Visible="false"></asp:Label>
</td>--%>
<td align="left">
<%# Eval("RowNum")%>
</td>
<td align="left">
<asp:Label ID="lblUserId" runat="server" Text='<%# Eval("UserId")%>' Visible="false"></asp:Label>
<asp:Label ID="lblFullName" runat="server" Text='<%# Eval("FirstName") + " " + Eval("LastName")%>'></asp:Label>
</td>
<td align="left">
<asp:Label ID="lblEmail" runat="server" Text='<%# Eval("EmailAddress")%>'></asp:Label>
</td>
<td align="left">
<%--<asp:Label ID="lblRole" runat="server" Text='<%# GetUserRole(Eval("RoleName"), Eval("DesignationName"))%>'></asp:Label>--%>
<asp:Label ID="lblRole" runat="server" Text='<%# Eval("RoleName")%>'></asp:Label>
</td>
<td align="left">
<%# Convert.ToString(Eval("Page")).Replace("~/","")%>
</td>
<td align="left">
<%# Eval("MacAddress")%>
</td>
<td align="left">
<%# Eval("IpAddress")%>
</td>
<td align="left">
<%# Convert.ToDateTime(Eval("CreatedDate")).ToString("MMM, dd yyyy")%>
</tr>
</ItemTemplate>
</asp:ListView>
代码背后的代码是
protected void lnk_Sort(object sender, EventArgs e)
{
LinkButton lnk = (LinkButton)sender;
string arg = lnk.CommandArgument.ToString();
ViewState["sortCol"] = arg;
GetSortDirection();
BindData(ViewState["sortCol"].ToString(), ViewState["sortDir"].ToString(), Convert.ToInt32(ViewState["nmbr"]), Pager.PageSize);
string name = lnk.ID;
Image img = (Image)(lstLogs.FindControl("img_" + name));
if (img != null)
{
SetSortOrderImage(img, ViewState["sortDir"].ToString());
}
}
private void SetSortOrderImage(Image image, String sortorder)
{
if (sortorder == "asc")
{
image.Visible = true;
image.ImageUrl = "../App_Themes/ThemeNew2/images/up.png";
}
else if (sortorder == "Desc")
{
image.Visible = true;
image.ImageUrl = "../App_Themes/ThemeNew2/images/down.png";
}
}
private void GetSortDirection()
{
if (Convert.ToString(ViewState["sortDir"]) == "Desc")
{
ViewState["sortDir"] = "asc";
}
else
{
ViewState["sortDir"] = "Desc";
}
}
试试这个