locked
Customized Sort Feature in my Listview Works once RRS feed

  • Question

  • User-1193791088 posted

    I added a button in the layout template of my listview.

    LayoutTemplate>
                                        <table runat="server" id="itemPlaceholderContainer" class="data-table">
                                            <tr class="thead-default bg-faded-primary">
                                                <th>&nbsp;</th>
                                                <th>
                                                    <asp:Button ID="Locations_SortByAddress" runat="server" OnClick="Locations_SortByAddress_Click" CssClass="btn btn-block" Text="Address"></asp:Button></th>
                                                <th>City</th>
                                                <th>State</th>
                                                <th>Zip</th>
                                            </tr>
                                            <tr runat="server" id="itemPlaceholder"></tr>
                                        </table>
                                    </LayoutTemplate>

    It calls this (I simplified it here to make more sense)

        protected void Locations_SortByAddress_Click(object sender, EventArgs e)
        {
            if (ViewState["StreetAddress"].ToString()=="ASC")
            {
                SqlData_AccountLocations.SelectCommand = "SELECT CAL.LocationId, CAL.LocationName, DLR.Relationship, L.StreetAddress, L.ZipCode, DLGUS.City, DLGUS.StateCode, CAL.Billing, L.GoogleMaps, L.Zillow, L.BingMaps FROM CuAccount_Locations AS CAL INNER JOIN Locations AS L ON CAL.LocationId = L.LocationId INNER JOIN Data_Locations_Relationship AS DLR ON CAL.RelationshipId = DLR.RelationshipId INNER JOIN Data_Locations_GeoDataUS AS DLGUS ON L.ZipCode = DLGUS.ZipCode WHERE (CAL.AccountId = @AccountId) ORDER BY CAL.Billing DESC, StreetAddress DESC";
                ViewState["StreetAddress"] = "DESC";
                ListView_AccountLocations.DataBind();
                Label5.Text = ViewState["StreetAddress"].ToString();
            }
            if (ViewState["StreetAddress"].ToString() == "DESC")
            {
                SqlData_AccountLocations.SelectCommand = "SELECT CAL.LocationId, CAL.LocationName, DLR.Relationship, L.StreetAddress, L.ZipCode, DLGUS.City, DLGUS.StateCode, CAL.Billing, L.GoogleMaps, L.Zillow, L.BingMaps FROM CuAccount_Locations AS CAL INNER JOIN Locations AS L ON CAL.LocationId = L.LocationId INNER JOIN Data_Locations_Relationship AS DLR ON CAL.RelationshipId = DLR.RelationshipId INNER JOIN Data_Locations_GeoDataUS AS DLGUS ON L.ZipCode = DLGUS.ZipCode WHERE (CAL.AccountId = @AccountId) ORDER BY CAL.Billing DESC, StreetAddress ASC";
                ViewState["StreetAddress"] = "ASC";
                ListView_AccountLocations.DataBind();
                Label5.Text = ViewState["StreetAddress"].ToString();
            }
        }

    The first time I click the button it works - Label5 says ASC

    but then will not accept a click again like it is disabled

    Friday, February 23, 2018 10:19 PM

All replies

  • User409696431 posted

    Where/when did you set the ViewState["StreetAddress"] to DESC before the first click?  You are only showing us the code to toggle it.

    If you set the ViewState["StreetAddress"] to DESC such that it gets set back to DESC after postback, pushing the button won't appear to change anything because it will change from DESC to ASC again.

    Friday, February 23, 2018 10:31 PM
  • User-1193791088 posted

    I have tried everything from if Page!=Postback

    is there a simpler way than this? GridView doesn't provide me with the flexibility I need.

    Friday, February 23, 2018 11:25 PM
  • User409696431 posted

    You could try using the ListView.Sort feature. 

    https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.sort%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396   The example shows sort columns selected from a dropdown list, where you would simply name them.  It does show how you would keep the sort the same for one column, and switch the order for the second one.

    (If using ViewState is a problem, see if it works with a specific control on the page (use a HiddenField, for example).

    Older framework, and VB, but useful: http://www.4guysfromrolla.com/articles/011608-1.aspx 

    Saturday, February 24, 2018 12:41 AM
  • User3690988 posted

    Don't you want an else if, instead of 2 if's?  If ViewState["StreetAddress"] is "ASC", it gets set to DESC in the first if, so the second if executes and sets it back to ASC...

    if (ViewState["StreetAddress"].ToString()=="ASC")
            {
                SqlData_AccountLocations.SelectCommand = "SELECT CAL.LocationId, CAL.LocationName, DLR.Relationship, L.StreetAddress, L.ZipCode, DLGUS.City, DLGUS.StateCode, CAL.Billing, L.GoogleMaps, L.Zillow, L.BingMaps FROM CuAccount_Locations AS CAL INNER JOIN Locations AS L ON CAL.LocationId = L.LocationId INNER JOIN Data_Locations_Relationship AS DLR ON CAL.RelationshipId = DLR.RelationshipId INNER JOIN Data_Locations_GeoDataUS AS DLGUS ON L.ZipCode = DLGUS.ZipCode WHERE (CAL.AccountId = @AccountId) ORDER BY CAL.Billing DESC, StreetAddress DESC";
                ViewState["StreetAddress"] = "DESC";
                ListView_AccountLocations.DataBind();
                Label5.Text = ViewState["StreetAddress"].ToString();
            }
            if (ViewState["StreetAddress"].ToString() == "DESC")
            {
                SqlData_AccountLocations.SelectCommand = "SELECT CAL.LocationId, CAL.LocationName, DLR.Relationship, L.StreetAddress, L.ZipCode, DLGUS.City, DLGUS.StateCode, CAL.Billing, L.GoogleMaps, L.Zillow, L.BingMaps FROM CuAccount_Locations AS CAL INNER JOIN Locations AS L ON CAL.LocationId = L.LocationId INNER JOIN Data_Locations_Relationship AS DLR ON CAL.RelationshipId = DLR.RelationshipId INNER JOIN Data_Locations_GeoDataUS AS DLGUS ON L.ZipCode = DLGUS.ZipCode WHERE (CAL.AccountId = @AccountId) ORDER BY CAL.Billing DESC, StreetAddress ASC";
                ViewState["StreetAddress"] = "ASC";
                ListView_AccountLocations.DataBind();
                Label5.Text = ViewState["StreetAddress"].ToString();
            }
    

    Tuesday, February 27, 2018 2:39 PM