使用 DataBind 进行 asp.net ListView 排序
使用 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:LinkButton,添加 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我解决了我的问题。
我添加了一个事件来处理排序。该事件获取命令名称(数据列)并将其和排序方向传递到函数中,该函数将再次调用数据库对返回的结果进行排序并重新绑定到列表视图。
我必须创建一个视图状态来保存列表视图排序方向,因为由于某种原因,排序事件处理程序一直说排序方向是升序。
前端
后端
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
Back End
我想您可以尝试为
ListView
的Sorting
事件添加处理程序。在那里您将获得事件参数中的排序列和排序顺序。这可以很容易地用于构建特定的查询并将其绑定到列表。I guess you could try to add a handler for
ListView
'sSorting
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.因为根据排序表达式(您在标记中定义的),SqlDataSource 很可能会执行类似的操作(我不确定这是否正是它的作用)您在幕后:
或者
SqlDataSource
可能正在使用DataView
对DataTable
进行排序。SqlDataSource 可以执行此操作,因为默认情况下它使用 DataSet 来存储结果集 (或者文档是这样说的):
由于您选择手动绑定到数据集,因此您需要自己施展“魔法”;换句话说,您处理 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:
Or
SqlDataSource
may be using aDataView
to sort theDataTable
.SqlDataSource can do this because by default it uses a DataSet to store the result set (or so says the documentation):
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.