Click here to Skip to main content
12,349,716 members (27,823 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET
Hi frnds... i have a drop down list
<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 7-Nov-12 21:12pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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);
    }
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 12 Apr 2014
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100