Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
DROPDOWNLIST1: i have a table say "cat" with columns-id,category and categoryid... i successfully populated its value from the database.
DROPDOWNLIST2: Also have another table called "products" with columns-id,name,categoryid.i need to populate my DROPDOWNLIST2 based on categoryid i retrieve from DROPDOWNLIST1
i did it using C#,but i have one problem ie each time i choose an option in DROPDOWNLIST1 after the page load, only then the value of DROPDOWNLIST2 is updated.i need to avoid this hence i need code for the same in javascript.im new to javascrpt.please help me
here is my working code
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            bindcatdropdown();

        }
    }
    protected void bindcatdropdown()
    {

        DataTable table = new DataTable();
        using (SqlConnection con = new SqlConnection(constr))
        {
            string sql = "select category,categoryid from cat ORDER BY categoryid";
            using (SqlCommand cmd = new SqlCommand(sql, con))
            {
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    da.Fill(table);
                }
            }

        }
        DropDownList1.DataSource = table;
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, new ListItem("--Select--", "0"));
       }

C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
   {
       DataTable table = new DataTable();
       //int id = Convert.ToInt32(DropDownList1.SelectedValue);
       int id = int.Parse(DropDownList1.SelectedValue.ToString());
       using (SqlConnection con = new SqlConnection(constr))
       {
           string sql = "select name,categoryid from products WHERE categoryid=@cid ORDER BY categoryid";
           using (SqlCommand cmd = new SqlCommand(sql, con))
           {
               using (SqlDataAdapter da = new SqlDataAdapter(cmd))
               {
                   cmd.Parameters.AddWithValue("@cid", id);
                   da.Fill(table);
               }
           }

       }
       DropDownList2.DataSource = table;
       DropDownList2.DataBind();
   }
Posted
Comments
[no name] 25-Aug-14 6:32am    
not in javascript it's in jquery try to user jquery sample may help you to get ur requiremet

Hi,

As per my understanding, you are creating cascading dropdownlist

Please refer to the following links:
http://www.aspdotnet-suresh.com/2013/10/jquery-cascading-dropdown-list-in-aspnet.html[^]
I read this sometime ago. It is a good one.

Also check these out:
http://stackoverflow.com/questions/7437381/how-to-set-selected-value-of-dropdownlist-with-jquery[^]

http://aspsnippets.com/Articles/AJAX-Cascading-DropDownList-using-jQuery-in-ASP.Net.aspx[^]

For Reference : http://api.jquery.com/change/[^]

Hope this helps you. :)

Regards,
Praneet
 
Share this answer
 
You can't do this using JavaScript.

You have two options. You can go for Ajax Cascading DropDownList or do a jQuery Ajax call to the Code behind Web Method. Inside that, bind your DropDownList.

1. Ajax Cascading Dropdownlist Sample with database using asp.net[^]
2. BIND DROPDOWNLIST FROM DATATABLE USING AJAX WEBMETHOD/[^]
 
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