Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
selected value in dropdownlist and then show velue in another dropdownlist in asp.net
Posted 21-Aug-12 23:03pm
M@anish1.3K
Comments
gladiatron at 22-Aug-12 4:04am
   
your question is not clear, please elaborate. Be specific and put some code up.
Anuja Pawar Indore at 22-Aug-12 9:14am
   
why repost?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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>
        
 
        
 
        <asp:dropdownlist id="DropDownList2" runat="server" xmlns:asp="#unknown">
        </asp:dropdownlist>
 
cheers
  Permalink  
Comments
ridoy at 22-Aug-12 4:33am
   
good solution..+5
M@anish at 22-Aug-12 5:09am
   
thank u so much MR sandip
AshishChaudha at 22-Aug-12 5:17am
   
+5
Sandip.Nascar at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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[^]
  Permalink  
Comments
AshishChaudha at 22-Aug-12 5:17am
   
my +5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Make the dropdownlist Autopostback property to True.
Then inside Dropdownlist SelectedIndexChanged event, bind the value to another dropdownlist.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

This should give you some information you need
cascading dropdownlist [^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Try out the following code.
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
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 575
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,476
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,917


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 22 Aug 2012
Copyright © CodeProject, 1999-2014
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