开发者

Making a listview sortable when it doesn't contain a layouttemplate and built in codebehind

I have a page of checkboxes, I select a few of them, click submit, and go to a second page. on this page, I have a listview that is built using the checkboxes selected on the previous page. Now I'm wondering how i can add sortability to this listview.

In the past I would add CommandName='sort' CommandArgument='column' to the link on the header in the LayoutTemplate. But since my listview has it's header row outside of the listview, this doesn't seem to work. Any ideas on how I can implement this? This is what I have so far:

<!-- 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>

Then in the code behind:

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

I've tried to add a templatelayout in the listview, but that gives me an error on the line that binds the data (listview.databind() ), i'm assuming because this isn't possible.

Any way to get a sortable header row on this listview? Thanks in advance? Newbie, sorry.


Have you considered handling the ListView.Sorting event and then invoking the ListView.Sort method to fire the event?

You can add your sorting logic within the Sorting event handler and then invoke the Sort method like so (possibly from a LinkButton Click event in your generated header).

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

Edit

Here is a simple example. I assume you are using a SqlDataSource based on the naming of your DataSourceID.

My example uses a SqlDataSource to select a column named Item from a table named Items and then display the data in the ListView.

I created a Button which performs a Sort on the ListView specifying the Item column and the sort direction.

Markup

<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>

Code Behind

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);
}

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜