我如何在ListView中对LinkBut​​ton进行排序

时间:2012-07-23 09:43:54

标签: c# asp.net listview sorting active-directory

我的计划:

我有一个ListView,它从Active Directory获取数据。用户在TextBox中输入字符串(姓氏或其中的一部分)。比ListView列出所有AD用户与TextBox中的相同字符串。每一行(行)都有一个按钮“Anzeigen”以获得更多关于用户的信息。

ASPX:

<asp:ListView runat="server" ID="myListView">

        <LayoutTemplate>
            <table id="UserTable" runat="server" border="0" cellspacing="10" cellpadding="2">
                <tr runat="server" id="Tr1" style="background-color:#E5E5FE">
                    <th runat="server"><asp:LinkButton ID="lnkBenutzer" runat="server" CommandName="Sort" CommandArgument="Benutzer" onsorting="ListView1_Sorting">Benutzer</asp:LinkButton></th>
                    <th runat="server"><asp:LinkButton ID="lnkemail" runat="server" CommandName="Sort" CommandArgument="eMail" onsorting="ListView1_Sorting" >eMail</asp:LinkButton></th>
                    <th runat="server"><asp:LinkButton ID="lnkVorname" runat="server" CommandName="Sort" CommandArgument="Vorname" onsorting="ListView1_Sorting" >Vorname</asp:LinkButton></th>
                    <th runat="server"><asp:LinkButton ID="lnkNachname" runat="server" CommandName="Sort" CommandArgument="Nachname" onsorting="ListView1_Sorting" >Nachname</asp:LinkButton></th>
                    <th runat="server"><asp:LinkButton ID="lnkTelefon" runat="server" CommandName="Sort" CommandArgument="Telefon" onsorting="ListView1_Sorting"  >Telefon</asp:LinkButton></th>
                </tr>
                <tr runat="server" id="ItemPlaceholder">
                </tr>
            </table>
        </LayoutTemplate>

        <ItemTemplate>

            <tr runat="server"> 

                <td align="left" ><asp:Label ID="Label1" Text='<%# Eval("Benutzer") %>' runat="server" /></td>
                <td align="left"><asp:Label ID="Label2" Text='<%# Eval("eMail") %>' runat="server" /></td>
                <td align="left"><asp:Label ID="Label3" Text='<%# Eval("Vorname") %>' runat="server" /></td>
                <td align="left"><asp:Label ID="Label4" Text='<%# Eval("Nachname") %>' runat="server" /></td>
                <td align="left"><asp:Label ID="Label5" Text='<%# Eval("Telefon") %>' runat="server" /></td>

             <td align="left"><asp:Button ID="Button1" Text="Anzeigen" OnCommand="Button1_Command" CommandName="Anzeigen" CommandArgument="MyArgument" runat="server" /></td>

            </tr>

        </ItemTemplate>

        <AlternatingItemTemplate>

            <tr style="background-color:#EFEFEF"> 

                <td align="left" ><asp:Label ID="Label1" Text='<%# Eval("Benutzer") %>' runat="server" /></td>
                <td align="left"><asp:Label ID="Label2" Text='<%# Eval("eMail") %>' runat="server" /></td>
                <td align="left"><asp:Label ID="Label3" Text='<%# Eval("Vorname") %>' runat="server" /></td>
                <td align="left"><asp:Label ID="Label4" Text='<%# Eval("Nachname") %>' runat="server" /></td>
                <td align="left"><asp:Label ID="Label5" Text='<%# Eval("Telefon") %>' runat="server" /></td>

             <td align="left"><asp:Button ID="Button1" Text="Anzeigen" OnCommand="Button1_Command" CommandName="Anzeigen" CommandArgument="MyArgument" runat="server" /></td> 

            </tr>

        </AlternatingItemTemplate>

CS文件:

...

protected void ListView1_Sorting(object sender, ListViewSortEventArgs e)
        {
            //here???
        }

...

我想要的是什么:

如果用户单击LinkBut​​ton“Benutzer”,则列表必须按字母排序:PI查看网站http://www.codeproject.com/Articles/24570/Complete-ListView-in-ASP-NET-3-5,但在示例和下载文件中不存在此排序方法:(

现在我不知道如何对列表进行排序。

所以我用数据填写我的列表:

protected void btnBenutzerSuchen_Click(object sender, EventArgs e)
        {
            DirectoryEntry Entry = new DirectoryEntry("LDAP://" + "Domain");

            string filter = "(&(objectClass=user)(objectCategory=person)(cn=" + txtBenutzer.Text + "*))";

            DirectorySearcher Searcher = new DirectorySearcher(Entry, filter);

            var q = from s in Searcher.FindAll().OfType<SearchResult>()
                    select new
                    {
                        Benutzer = GetProperty(s, "sAMAccountName"),
                        eMail = GetProperty(s, "mail"),
                        Vorname = GetProperty(s, "givenName"),
                        Nachname = GetProperty(s, "sn"),
                        Telefon = GetProperty(s, "telephoneNumber")
                    };

            this.myListView.DataSource = q;
            this.myListView.DataBind();

        }

塔拉索夫

1 个答案:

答案 0 :(得分:0)

您希望根据dataname对数据进行排序。 为此,您必须将datakey传递给rowdataBound事件。

喜欢':

protected void gvEmployeeList_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "name")
    {
        int index = Convert.ToInt32(e.CommandArgument);
        GridViewRow selectedRow = gvEmployeeList.Rows[index];
        string deptID = gridview1.DataKeys[index].Value.ToString().Trim();
         DataTable dtEmplist = new DataTable();
        dtEmplist = getFilterEmployeeList(deptID);
        if (dtEmplist.Rows.Count > 0)
        {
            gridview1.DataSource = dtEmplist;
            gridview1.DataBind();
        }
        else
        {
            lblMsg.Text = "No Data Available";
        }
    }
}

获取数据密钥

按(GridViewSortEventArgs e)

排序

e.sortExpression属性。

它会帮助你我的朋友......