Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi frnds... i have a drop down list
XML
<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:ListItem>Highest Discount</asp:ListItem>
                    </asp:DropDownList>



and Datalist control
i have to sort DaTALIST by selecting these items from dropdown

please help me friends
Posted

C#
private void showData(string sortDirection)
    {
        //get your data in desired sorting and bind to data list here

    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        showData(DropDownList1.SelectedValue);
    }
 
Share this answer
 
C#
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["ProBizConnection"].ConnectionString;
        SqlConnection myConnect = new SqlConnection(strConnectionString);

        string strCommandText ="SELECT * FROM [Category ] WHERE catID<= 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["ProBizConnection"].ConnectionString;
        SqlConnection myConnect = new SqlConnection(strConnectionString);

        string strCommandText = "SELECT catID, packageName, price, description1, description2, image1, image2 FROM Category WHERE catID <= 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["ProBizConnection"].ConnectionString;
        SqlConnection myConnect = new SqlConnection(strConnectionString);

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

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

   reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

    }
    return reader;
}
 
Share this answer
 
v2
Comments
Member 10293675 12-Apr-14 1:34am    
I think this will help you to solve your problem

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900