Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
selected value in dropdownlist and then show velue in another dropdownlist in asp.net
Posted
Comments
I.explore.code 22-Aug-12 4:04am    
your question is not clear, please elaborate. Be specific and put some code up.
Anuja Pawar Indore 22-Aug-12 9:14am    
why repost?

Here is the code for you.
To fill up the database, call the database to populate dropdown.

code behind
protected void Page_Load(object sender, EventArgs e)
{
    DropDownList1.Items.Add(new ListItem("Sandip", "1"));
    DropDownList1.Items.Add(new ListItem("Jon", "2"));
    DropDownList1.Items.Add(new ListItem("Michael", "3"));
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList2.Items.Clear();
    if (DropDownList1.SelectedIndex == 0)
    {
        DropDownList2.Items.Add(new ListItem("DOT.NET", "1"));
        DropDownList2.Items.Add(new ListItem("C#", "2"));
        DropDownList2.Items.Add(new ListItem("ASP.NET", "3"));
        DropDownList2.Items.Add(new ListItem("LINQ", "4"));
    }
    else if (DropDownList1.SelectedIndex == 1)
    {
        DropDownList2.Items.Add(new ListItem("DOT.NET", "1"));
        DropDownList2.Items.Add(new ListItem("VB.NET", "2"));
        DropDownList2.Items.Add(new ListItem("ASP.NET", "3"));
    }
    else if (DropDownList1.SelectedIndex == 2)
    {
        DropDownList2.Items.Add(new ListItem("PHP", "1"));
        DropDownList2.Items.Add(new ListItem("MYSQL", "2"));
    }

}


aspx code
<asp:dropdownlist id="DropDownList1" runat="server" autopostback="True" xmlns:asp="#unknown">
    onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:dropdownlist>
<br />
<br />
<asp:dropdownlist id="DropDownList2" runat="server" xmlns:asp="#unknown">
</asp:dropdownlist>


cheers
 
Share this answer
 
Comments
ridoy 22-Aug-12 4:33am    
good solution..+5
M@anish 22-Aug-12 5:09am    
thank u so much MR sandip
AshishChaudha 22-Aug-12 5:17am    
+5
Sandip.Nascar 22-Aug-12 5:24am    
one thing I missed to mention.
If you do some repeatetive selection in 1st drop down, you will see the items in the drop down are increasing.

The reason is viewstate.
To cope up with this situation, add this code

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.Items.Add(new ListItem("Sandip", "1"));
DropDownList1.Items.Add(new ListItem("Jon", "2"));
DropDownList1.Items.Add(new ListItem("Michael", "3"));
}
}

thx
Member 13214582 26-May-17 9:35am    
still its showing same repetition please do the needful
This is called cascading dropdownlist. here is an article that presents 2 approaches to do the same. contains sample projects too. refer it and see if it could be helpful

AJAX for beginners (Part 2) - Using XMLHttpRequest and jQuery AJAX to implement a cascading dropdown[^]
 
Share this answer
 
Comments
AshishChaudha 22-Aug-12 5:17am    
my +5
This should give you some information you need
cascading dropdownlist [^]
 
Share this answer
 
Make the dropdownlist Autopostback property to True.
Then inside Dropdownlist SelectedIndexChanged event, bind the value to another dropdownlist.
 
Share this answer
 
Try out the following code.
C#
string strSql="select [id],[name] from [tablename1]";
//write code here for fetching data
dropdownlist1.datasource=ds;
dropdownlist1.DataTextField="name";
dropdownlist1.DataValueField="id";
dropdownlist1.databind();
ds.Clear();


protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
  string strSql1="select [id],[name] from [tablename2] where id='"+ dropdownlist1.SelectedValue +"'";
//write code here for fetching data
dropdownlist2.datasource=ds1;
dropdownlist2.DataTextField="name";
dropdownlist2.DataValueField="id";
dropdownlist2.databind();
ds1.Clear();
}


Thanks
Ashish
 
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