使用 DataBind 进行 asp.net ListView 排序

发布于 2024-12-06 21:04:36 字数 2011 浏览 0 评论 0原文

使用 LayoutTemplate 中的列标题对列表视图进行排序

我可以使用 asp:SqlDataSource 对基本列表视图进行排序,并通过将列表视图属性 DataSourceID 指向 asp:SqlDataSource ID 来设置它。当不使用 asp:SqlDataSource 且仅使用后面代码中的 DataBinding 时,我在排序时遇到问题。

SqlDataSource 示例:

<asp:ListView ID="ContactsListView" DataSourceID="ContactsDataSource" runat="server">
    <LayoutTemplate>
        <table width="640px" runat="server">
            <tr class="header" align="center" runat="server">
                <td>
                    <asp:LinkButton runat="server" ID="SortByFirstNameButton" CommandName="Sort" Text="First Name" CommandArgument="FirstName" />
    </LayoutTemplate>
    ....
</asp:ListView>

<asp:SqlDataSource ID="ContactsDataSource" runat="server" 
    ConnectionString="<%$ ConnectionStrings:MainConnString %>"
    SelectCommand="SELECT * FROM TableName">
</asp:SqlDataSource>

DataBind 示例:

<asp:ListView ID="ContactsListView" DataSourceID="ContactsDataSource" runat="server">
    <LayoutTemplate>
        <table width="640px" runat="server">
            <tr class="header" align="center" runat="server">
                <td>
                    <asp:LinkButton runat="server" ID="SortByFirstNameButton" CommandName="Sort" Text="First Name" CommandArgument="FirstName" />
    </LayoutTemplate>
    ....
</asp:ListView>

protected void Page_Load(object sender, EventArgs e)
{
    String SQL = "SELECT * FROM Customer";
    SqlDataAdapter da= new SqlDataAdapter(SQL, ConnStr);
    DataSet ds = new DataSet();
    da.Fill(ds);

    ContactsListView.DataSource = ds.Tables[0];
    ContactsListView.DataBind();
}

两个代码示例都填充列表视图,但第二个示例数据绑定不适用于排序。对于第一个示例,排序仅适用于 LayoutTemplate 中添加的 asp:LinkBut​​ton,添加 CommandName="sort" 并设置 CommandArugment="ColumnName",但不适用于第二个示例。

谁能解释一下为什么以及如何使用 DataBind 方法背后的代码进行排序?

谢谢!

Sort listview using column headings in the LayoutTemplate

I am able to sort a basic list view using asp:SqlDataSource and setting the list view property DataSourceID by pointing it to the asp:SqlDataSource ID. I am having an issue when sorting when not using the asp:SqlDataSource and just DataBinding from the code behind.

SqlDataSource Example:

<asp:ListView ID="ContactsListView" DataSourceID="ContactsDataSource" runat="server">
    <LayoutTemplate>
        <table width="640px" runat="server">
            <tr class="header" align="center" runat="server">
                <td>
                    <asp:LinkButton runat="server" ID="SortByFirstNameButton" CommandName="Sort" Text="First Name" CommandArgument="FirstName" />
    </LayoutTemplate>
    ....
</asp:ListView>

<asp:SqlDataSource ID="ContactsDataSource" runat="server" 
    ConnectionString="<%$ ConnectionStrings:MainConnString %>"
    SelectCommand="SELECT * FROM TableName">
</asp:SqlDataSource>

DataBind Example:

<asp:ListView ID="ContactsListView" DataSourceID="ContactsDataSource" runat="server">
    <LayoutTemplate>
        <table width="640px" runat="server">
            <tr class="header" align="center" runat="server">
                <td>
                    <asp:LinkButton runat="server" ID="SortByFirstNameButton" CommandName="Sort" Text="First Name" CommandArgument="FirstName" />
    </LayoutTemplate>
    ....
</asp:ListView>

protected void Page_Load(object sender, EventArgs e)
{
    String SQL = "SELECT * FROM Customer";
    SqlDataAdapter da= new SqlDataAdapter(SQL, ConnStr);
    DataSet ds = new DataSet();
    da.Fill(ds);

    ContactsListView.DataSource = ds.Tables[0];
    ContactsListView.DataBind();
}

Both code samples populate the list view, but the second example data binding does not work for sorting. With the first example, the sorting just works with the added asp:LinkButton in the LayoutTemplate adding the CommandName="sort" and setting the CommandArugment="ColumnName", but it does not work with the second example.

Can anyone please explain why and how to get the sorting working using the code behind DataBind method?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

燃情 2024-12-13 21:04:36

我解决了我的问题。

我添加了一个事件来处理排序。该事件获取命令名称(数据列)并将其和排序方向传递到函数中,该函数将再次调用数据库对返回的结果进行排序并重新绑定到列表视图。

我必须创建一个视图状态来保存列表视图排序方向,因为由于某种原因,排序事件处理程序一直说排序方向是升序。

前端

<asp:ListView ID="ContactsListView" OnSorting="ContactsListView_Sorting" runat="server">
    <LayoutTemplate>
        <table width="640px" runat="server">
            <tr class="header" align="center" runat="server">
                <td>
                    <asp:LinkButton runat="server" ID="SortByFirstNameButton" CommandName="Sort" Text="First Name" CommandArgument="FirstName" />
    </LayoutTemplate>
    ....
</asp:ListView>

后端

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        BindContacts(string.Empty);
    }
}

protected SortDirection ListViewSortDirection
{
    get
    {
        if (ViewState["sortDirection"] == null)
            ViewState["sortDirection"] = SortDirection.Ascending;
        return (SortDirection)ViewState["sortDirection"];
    }
    set { ViewState["sortDirection"] = value; }
}

protected void ContactsListView_Sorting(Object sender, ListViewSortEventArgs e)
{
    BindContacts(e.SortExpression + " " + ListViewSortDirection.ToString());

    // Check the sort direction to set the image URL accordingly.
    string imgUrl;
    if (ListViewSortDirection == SortDirection.Ascending)
        ListViewSortDirection = SortDirection.Descending;
    else
        ListViewSortDirection = SortDirection.Ascending;
}

private void BindContacts(string sortExpression)
{
    sortExpression = sortExpression.Replace("Ascending", "ASC");
    sortExpression = sortExpression.Replace("Descending", "DESC");
    using (SqlConnection conn = new SqlConnection(_connStr))
    {
        conn.Open();
        using (SqlDataAdapter dAd = new SqlDataAdapter("SELECT * FROM Customer", conn))
        {
            DataTable dTable = new DataTable();
            dAd.Fill(dTable);
            // Sort now
            dTable.DefaultView.Sort = sortExpression;
            // Bind data now
            ContactsListView.DataSource = dTable;
            ContactsListView.DataBind();
        }
        conn.Close();
    }
}

I solved my issue.

I added an event to handle the sorting. The event grabs the command name (Data column) and passes it and the sorting direction into a function which will make another call to the database sort the returned results and rebind to the List View.

I had to create a view state to hold the List View Sort Direction because for some reason, the onsorting event handler kept saying that the sort direction was ascending.

Front End

<asp:ListView ID="ContactsListView" OnSorting="ContactsListView_Sorting" runat="server">
    <LayoutTemplate>
        <table width="640px" runat="server">
            <tr class="header" align="center" runat="server">
                <td>
                    <asp:LinkButton runat="server" ID="SortByFirstNameButton" CommandName="Sort" Text="First Name" CommandArgument="FirstName" />
    </LayoutTemplate>
    ....
</asp:ListView>

Back End

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        BindContacts(string.Empty);
    }
}

protected SortDirection ListViewSortDirection
{
    get
    {
        if (ViewState["sortDirection"] == null)
            ViewState["sortDirection"] = SortDirection.Ascending;
        return (SortDirection)ViewState["sortDirection"];
    }
    set { ViewState["sortDirection"] = value; }
}

protected void ContactsListView_Sorting(Object sender, ListViewSortEventArgs e)
{
    BindContacts(e.SortExpression + " " + ListViewSortDirection.ToString());

    // Check the sort direction to set the image URL accordingly.
    string imgUrl;
    if (ListViewSortDirection == SortDirection.Ascending)
        ListViewSortDirection = SortDirection.Descending;
    else
        ListViewSortDirection = SortDirection.Ascending;
}

private void BindContacts(string sortExpression)
{
    sortExpression = sortExpression.Replace("Ascending", "ASC");
    sortExpression = sortExpression.Replace("Descending", "DESC");
    using (SqlConnection conn = new SqlConnection(_connStr))
    {
        conn.Open();
        using (SqlDataAdapter dAd = new SqlDataAdapter("SELECT * FROM Customer", conn))
        {
            DataTable dTable = new DataTable();
            dAd.Fill(dTable);
            // Sort now
            dTable.DefaultView.Sort = sortExpression;
            // Bind data now
            ContactsListView.DataSource = dTable;
            ContactsListView.DataBind();
        }
        conn.Close();
    }
}
放我走吧 2024-12-13 21:04:36

我想您可以尝试为 ListViewSorting 事件添加处理程序。在那里您将获得事件参数中的排序列和排序顺序。这可以很容易地用于构建特定的查询并将其绑定到列表。

I guess you could try to add a handler for ListView's Sorting event. There you are given the sorting column and the sort order in the event arguments. This could be easily usable to build a specific query and bind it to the list.

老街孤人 2024-12-13 21:04:36

因为根据排序表达式(您在标记中定义的),SqlDataSource 很可能会执行类似的操作(我不确定这是否正是它的作用)您在幕后:

Expression<Func<DataRow,object>> myExpression = row => row["SortExpressionYouDefinedForTheColumn"];
IEnumerable<DataRow> ex = ds.Tables[0].AsEnumerable().OrderBy(myExpression.Compile());

或者 SqlDataSource 可能正在使用 DataViewDataTable 进行排序。

SqlDataSource 可以执行此操作,因为默认情况下它使用 DataSet 来存储结果集 (或者文档是这样说的):

数据检索模式标识 SqlDataSource 控件如何从底层数据库检索数据。

当 DataSourceMode 属性设置为 DataSet 值时,数据为
加载到 DataSet 对象中并存储在服务器上的内存中。这
支持用户界面控件(例如 GridView)的场景
提供排序、过滤和分页功能..

由于您选择手动绑定到数据集,因此您需要自己施展“魔法”;换句话说,您处理 OnSort 命令,获取排序表达式,再次获取数据源(无论您如何执行,从会话或再次调用数据库),然后与上面显示的行类似地进行排序,然后重新绑定到您的 Gridview 。

Because depending on the sort expression (which you defined in your markup) SqlDataSource will very likely do something like this (I'm not sure this is exactly what it does) for you behind the scenes:

Expression<Func<DataRow,object>> myExpression = row => row["SortExpressionYouDefinedForTheColumn"];
IEnumerable<DataRow> ex = ds.Tables[0].AsEnumerable().OrderBy(myExpression.Compile());

Or SqlDataSource may be using a DataView to sort the DataTable.

SqlDataSource can do this because by default it uses a DataSet to store the result set (or so says the documentation):

The data retrieval mode identifies how a SqlDataSource control retrieves data from the underlying database.

When the DataSourceMode property is set to the DataSet value, data is
loaded into a DataSet object and stored in memory on the server. This
enables scenarios where user interface controls, such as GridView,
offer sorting, filtering, and paging capabilities..

Since you chose to bind manually to a DataSet, you need to do the "magic" yourself; in other words, you handle the OnSort command, get the sort expression, get your data source again (however you do it, from Session or by calling the database again) and do your sort similarly to the lines shown above and rebind to your Gridview.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文