2011-09-06 54 views
1

我有一个复选框页面,我选择它们中的一些,单击submit,然后转到第二页。在这个页面上,我有一个使用前一页选择的复选框构建的列表视图。现在我想知道如何为此列表视图添加排序功能。当它不包含layouttemplate并且内置代码隐藏时,使listview可排序

在过去,我会将CommandName ='sort'CommandArgument ='column'添加到LayoutTemplate中标题的链接上。但是由于我的listview在列表视图之外有它的标题行,所以这似乎不起作用。关于如何实现这一点的任何想法?这是我到目前为止有:

<!-- header row (outside of listview, when I try to put it as a LayoutTemplate in listview i get an error, see below) --> 
<table> 
    <tr><% For Each i As String In Request.Form 
      If i.IndexOf("checkbox_") = 0 Then 
       Response.Write("<th>" & i.Substring(Len("checkbox_")) & "</th>") 
      End If 
     Next %></tr> 
</table> 

<!-- Then the listview: --> 
<asp:ListView runat="server" ID="ReportListView" DataSourceID="ReportListViewSDS"> 
    <ItemTemplate> 
    <table> 
     <tr> 
      <asp:PlaceHolder runat="server" ID="itemPlaceHolder" /> 
     </tr> 
    </table> 
    </ItemTemplate> 
</asp:ListView> 
在后面的代码

然后:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    ' This works fine: 
    ' Get the Request Vars that are checkboxes, and build the sql command. 
    ' Run the sql command 
    ' Databind() 
End Sub 

' while binding the data, build the itemPlaceholder, to contain the contents of what's returned in the sql: 
Protected Sub ReportListView_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles ReportListView.ItemDataBound 

    If (e.Item.ItemType = ListViewItemType.DataItem) Then 
     Dim plc As PlaceHolder = DirectCast(e.Item.FindControl("itemPlaceHolder"), PlaceHolder) 
     Dim di As Data.DataRowView = e.Item.DataItem() 

     For Each c In di.Row.ItemArray 
      Dim l As New Literal 
      l.Text = String.Format("<td class='customreport'>{0}</td>", c.ToString) 
      plc.Controls.Add(l) 
     Next 
    End If 
End Sub 

我一直在努力,在列表视图中添加templatelayout,但给我就行了一个错误,绑定数据(listview.databind()),我假设,因为这是不可能的。

任何方式来获得这个列表视图上的可排序标题行?提前致谢?新手,对不起。

回答

1

您是否考虑过处理ListView.Sorting事件,然后调用ListView.Sort方法来触发事件?

您可以在排序事件处理程序中添加排序逻辑,然后像这样调用排序方法(可能来自您生成的标题中的LinkBut​​ton Click事件)。

ReportListView.Sort("COLUMN", SortDirection.Ascending); 

编辑

下面是一个简单的例子。我假设你正在使用基于你的DataSourceID的命名的SqlDataSource。

我的示例使用SqlDataSource从名为Items的表中选择名为Item的列,然后在ListView中显示数据。

我创建了一个按钮,它对ListView指定Item列和排序方向进行排序。

标记

<asp:Button ID="button1" runat="server" Text="Sort" OnClick="button1_Click" /> 
<asp:ListView ID="listView1" runat="server" DataSourceID="sqlDataSource"> 
    <ItemTemplate> 
     <div><%# Eval("Item") %></div> 
    </ItemTemplate> 
</asp:ListView> 
<asp:SqlDataSource ID="sqlDataSource" runat="server" ConnectionString="Data Source=.;Initial Catalog=******;Integrated Security=SSPI;" SelectCommand="SELECT Item FROM Items"> 
</asp:SqlDataSource> 

代码隐藏

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     Session["SortDirection"] = SortDirection.Ascending; 
    } 
} 

protected void button1_Click(object sender, EventArgs e) 
{ 
    SortDirection sortDirection = (SortDirection)Session["SortDirection"] == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending; 
    Session["SortDirection"] = sortDirection; 

    listView1.Sort("Item", sortDirection); 
} 

希望这有助于。

+0

是的,我确实尝试过,但没有得到它的工作。你可能有一个工作的例子,或更多的细节?我检查了这些页面,并试图准确地执行代码。问题是当我点击应该排序的链接按钮时,页面只刷新,排序不适用。有任何想法吗?谢谢! – russds

+0

我认为问题在于Linkbuttons不在Listview中。因为我添加了“CommandName”=排序到listview之外的链接按钮,这可能就是为什么它们没有任何效果。再一次,它回到没有LayoutTemplate。因为我刚刚在列表视图之外创建了一个表来充当我的表头。 – russds

相关问题