Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi, I got a drop down list. User upon selecting different value, I should get the option text of the option user has selected from the dropdown list, then go find the data from the database and display the relevant content on the webpage. If user change the option again, I should go retrieve different data based on option text selected and display the different content on the webpage.

How could I accomplish this? Anyone has any idea to keep me started? I researched online but couldn't find anything useful.

This is my codes:
ASP.NET
//Aspx file:
            <asp:Dropdownlist ID="SelectionDropDownList" runat="server" Width="136px" EnableViewState="True" AppendDataBoundItems="true">
            </asp:Dropdownlist>

//Cs file:
//how the dropdown list is being populated out. Dropdown list is being populated out from what the user has selected from a listbox.

public void BindSomething()
        {
DateTime choosenDate = DateTime.MinValue;

            using (SqlConnection conn = new SqlConnection(dbConn))
            {
                using (SqlCommand cmd = new SqlCommand(spretrieve, conn))
                {
                    //Lost to hold the values
                    List<DateTime> listCopy = new List<DateTime>();
                    DateTime dt;
                    string values = String.Join(", ", ListBox1.Items.Cast<ListItem>().Where(i => i.Selected).Select(i => i.Text));
                    if (values.Contains("Select All"))
                    {
                        //Loop through each items in listbox and then add it to list
                        foreach (ListItem li in ListBox1.Items)
                        {
                            if (DateTime.TryParse(li.Text, out dt))
                            {
                                listCopy.Add(dt);
                            }
                        }
                    }
                    else
                    {
                        //Loop through each items in listbox and then add it to list
                        foreach (ListItem li in ListBox1.Items)
                        {
                            //check if item is selected
                            if (li.Selected == true)
                            {
                                //add items to list
                                listCopy.Add(DateTime.Parse(li.Text));
                            }
                        }
                    }

                    //compare and sort so that the latest date comes on top
                    listCopy.Sort((x, y) => y.CompareTo(x));
                    //clear the items in dropdownlist
                    SelectionDropDownList.Items.Clear();
                    //set the datasource to dropdownlist
                    SelectionDropDownList.DataSource = listCopy;
                    //set the dateformatstring in dropdownlist
                    SelectionDropDownList.DataTextFormatString = "{0:dd-MMM-yyyy}";
                    //Bind the dropdownlist
                    SelectionDropDownList.DataBind();
}


Appreciate if someone can help me on this, thanks a lot!!
Posted
Comments
Palash Mondal_ 29-Sep-15 4:32am    
If you change the content of the page then the dropdown would be removed from the page right, as new content would replace it. Then how can user change the dropdown option to something else?
Sinisa Hajnal 29-Sep-15 8:00am    
Use jQuery.ajax to get the data and put it in predefined content holder. Alternative, use asp:UpdatePanel (which uses Ajax internally) to get selected URL.

1 solution

Check the two links below on how to change page content based on the drop down select change. You need to use jQuery ajax $.Get methods to get data from the server.

http://stackoverflow.com/questions/6164507/change-the-content-of-a-div-based-on-selection-from-drop-down-menu[^]

https://api.jquery.com/jquery.get/[^]
 
Share this answer
 

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