Page

How to Sort Datalist Using Dropdownlist in ASP.Net


// Example.aspx //

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Example.aspx.cs" Inherits="_Example" %>  
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title> Sort Datalist Using Dropdownlist</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  

<asp:DropDownList ID="DDLSorting" runat="server" AutoPostBack="True" onselectedindexchanged="DDLSorting_SelectedIndexChanged">
                        <asp:ListItem>Best Match</asp:ListItem>
                        <asp:ListItem>New Arrivals</asp:ListItem>
                        <asp:ListItem Value="3" Text="Price: Low to High"></asp:ListItem>
                        <asp:ListItem>Price: High to Low</asp:ListItem>
                        </asp:DropDownList>
              </div>  
        </form>  
    </body>  
</html>  


// Example.aspx.cs //


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

public partial class Example : System.Web.UI.Page
{

  protected void Page_Load(object sender, EventArgs e)
    {
    }

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    bindDropDownList();
}

private void bindDropDownList()
{
    DropDownList1.DataTextField = "price";
    DataList1.DataSourceID = null;
    DataList1.DataSource = getReader();
    DropDownList1.DataBind();

}

private SqlDataReader getReader()
{
    SqlDataReader reader = null;

    if(DropDownList1.Text == "-Select-")
    {
      string strConnectionString =
            ConfigurationManager.ConnectionStrings["Pubs"].ConnectionString;
        SqlConnection myConnect = new SqlConnection(strConnectionString);

        string strCommandText ="SELECT * FROM [Category ] WHERE ID<= 20";

        SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
        myConnect.Open();

   reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

    }
    else if (DropDownList1.SelectedValue == "Price - Highest to Lowest")
    {
        string strConnectionString =
            ConfigurationManager.ConnectionStrings["Pubs"].ConnectionString;
        SqlConnection myConnect = new SqlConnection(strConnectionString);

        string strCommandText = "SELECT ID, packageName, price, description1, description2, image1, image2 FROM Category WHERE ID <= 20 ORDER BY price desc";

        SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
        myConnect.Open();

   reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

    }

    else if (DropDownList1.DataTextField == "Price - Lowest to Highest")
    {
        string strConnectionString =
            ConfigurationManager.ConnectionStrings["Pubs"].ConnectionString;
        SqlConnection myConnect = new SqlConnection(strConnectionString);

        string strCommandText = "SELECT ID, packageName, price, description1, description2, image1, image2 FROM Category WHERE ID <= 20 ORDER BY price";

        SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
        myConnect.Open();

   reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

    }
    return reader;
   }
}

No comments:

Post a Comment