Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I solved my issue.</p> <p>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.</p> <p>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.</p> <p><strong>Front End</strong></p> <pre><code>&lt;asp:ListView ID="ContactsListView" OnSorting="ContactsListView_Sorting" runat="server"&gt; &lt;LayoutTemplate&gt; &lt;table width="640px" runat="server"&gt; &lt;tr class="header" align="center" runat="server"&gt; &lt;td&gt; &lt;asp:LinkButton runat="server" ID="SortByFirstNameButton" CommandName="Sort" Text="First Name" CommandArgument="FirstName" /&gt; &lt;/LayoutTemplate&gt; .... &lt;/asp:ListView&gt; </code></pre> <p><strong>Back End</strong></p> <pre><code>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(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload