Click here to Skip to main content
15,911,035 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I need some help I have 2 dropdownlist

in the first one there's some values ,I need when I choose value in the first drop the second drop fill with the things related to what I selected


just how to write this code in first drop what the code will be ?????



thanx alot
Posted
Comments
Al Moje 22-Dec-11 20:54pm    
How far have you done? Please show us your code...

Try this,

ASP.NET
<asp:dropdownlist autopostback="true" id="dd1" runat="server" onselectedindexchanged="dd_select_changed" xmlns:asp="#unknown">

       </asp:dropdownlist>
       <asp:dropdownlist id="dd2" runat="server" xmlns:asp="#unknown">
       </asp:dropdownlist>


CS

C#
 protected void Page_Load(object sender, EventArgs e)
   {
connection();
       string sql = "select * from tblCountry ";
       cmd = new SqlCommand(sql, conn);
       da = new SqlDataAdapter(cmd);
       da.Fill(ds);


           for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
           {
               dd1.DataSource = ds.Tables[0];
               dd1.DataBind();
               dd1.DataTextField = "CountryName";

               dd1.DataValueField = "CountryName";
           }
  }


protected void dd_select_changed(object sender, EventArgs e)
   {
       connection();
       string sql2 = "select * from tblCity where Country='" + dd1.SelectedItem.Value.ToString() + "'";
       cmd = new SqlCommand(sql2, conn);
       da = new SqlDataAdapter(cmd);
       DataSet ds2 = new DataSet();
       da.Fill(ds2);
           dd2.DataSource = ds2.Tables[0];
           dd2.DataBind();
       dd2.DataTextField = "CityName";
       dd2.DataValueField = "CityName";
   }

tblCountry

SQL
CountryName

India
US
UK


tblCity
SQL
CityName        Country

London	        UK
Delhi	        India
san francisco	US
 
Share this answer
 
Comments
Sarah Mohammed77 23-Dec-11 19:39pm    
Really thanx alot but just simple question what does Tables[0] mean ????
Shobana16 25-Dec-11 4:29am    
Dataset returns the values in the form of tables . So if we assign more than one select query for dataset it returns tables [0] for first select values tables [1] for second.like wise. For example
create procedure sp_Example
begin
select * from tblCountry

select * from tblCity

end

// tblCountry result will be in tables [0] .
tblCity result set will be in tables[1]
Please use below solution:

<div>
       <asp:dropdownlist id="ddl1" runat="server" autopostback="true" xmlns:asp="#unknown">
           onselectedindexchanged="ddl1_SelectedIndexChanged">
           <asp:listitem text="1" value="1"></asp:listitem>
           <asp:listitem text="2" value="2"></asp:listitem>
       </asp:dropdownlist>
       <asp:dropdownlist id="ddl2" runat="server" xmlns:asp="#unknown">
       </asp:dropdownlist>
   </div>


at code behind

protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Now here you can get selected value.
        //ddl1.SelectedItem.Text  

        if (ddl1.SelectedItem.Text == "1")
        { 
        
            ddl2.Items.Insert(0,new ListItem("test1","test1"));
            ddl2.Items.Insert(0,new ListItem("test21","test21"));
        }
        else if (ddl1.SelectedItem.Text == "2")
        {

            ddl2.Items.Insert(0, new ListItem("test2", "test2"));
            ddl2.Items.Insert(0, new ListItem("test22", "test22"));
        }

        // And also you can fill it using  data table or get the value from the database

    }



Let me know this solution work for you :)
 
Share this answer
 
In ASP .NET, you can use the below JQuery code. Itz generic solution.

JavaScript
<select id="mainDropDown" onchange="getSubcatValue()">
     //Options for mainDropDown list
</select>

<select id="subDropDown"></select>

<script type="text/javascript>
    function getSubcatValue()
    {
        var chosenValue = $("#mainDropDown").val();
        var options = $('#subDropDown').prop('options');
        options = [];
        if (chosenValue == "something") {
            for (var i = 0 ; i < 10 ; i++) {
                options[i] = new Option(i+1, i); // populate the subDropDown with 10 options: 1, 2 ,3, 4, etc...
                //The 1st parameter is the text for the option, the 2nd parameter is the value of the option
            }
        }
    }
</script>
 
Share this answer
 
instead of dropdown you used list box.
if dropdown list complessory then paste your code upto which you are done then we will solve it!.
 
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