?
Solved

Set SelectedValue on a dropdownlist after selecting a value in other dropdownlist

Posted on 2010-11-24
9
Medium Priority
?
618 Views
Last Modified: 2013-12-17
Hi,

I have 2 dropdownlists, 1 for the city and 1 for the postal code. When the page loads, the 2 dropdowns are filled from a database table (they are set up the same way).

When I select the city i want the corresponding postal code selected in the dropdown with the postal codes. How can this be done? It should also work the in the other way.

Attached you will find the code for as far i got.


Kind regards,
Maarten
*.aspx file
-----------
<asp:DropDownList ID="ddlBillToCity" style="width: 227px;" runat="server" 
                                AutoPostBack="True" onselectedindexchanged="ddlBillToCity_SelectedIndexChanged"></asp:DropDownList>

<asp:DropDownList ID="ddlBillToPostCode" style="width: 227px;" runat="server" 
                                AutoPostBack="True" OnSelectedIndexChanged="ddlBillToPostCode_SelectedIndexChanged"></asp:DropDownList>

*.aspx.cs file
--------------
protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            
        }
        else
        {
            //if no postback, then there can't be a table in the session
            // so we create and save it's layout here
            PostalCodeList = new DataTable();
            PostalCodeList.Columns.Add("Code");
            PostalCodeList.Columns.Add("City");
            PostalCodeList.Columns.Add("CodeCity");
            Session["PostalCodeList"] = PostalCodeList;

            ////Fill the PostalCodeList & DropDownList ddlBillToPostCode & DropDownList ddlBillToCity
            FillPostalCodeListFromDB();
        }
    }

protected void ddlBillToCity_SelectedIndexChanged(object sender, EventArgs e)
    {
        
    }

    protected void ddlBillToPostCode_SelectedIndexChanged(object sender, EventArgs e)
    {
        
    }

Open in new window

0
Comment
Question by:maartendierckxsens
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 34206810
You must work with the selectedIndex_change event of each one
0
 
LVL 13

Accepted Solution

by:
Jesus Rodriguez earned 250 total points
ID: 34206922
You can do this
- Add an ID fiel to the Columns of the Datatable
- Set the value for each dropdownlist value as the IdColumn
- Set the DisplayField member for the first dropdown as the Code colum
- Set the DisplayField member for the second dropdown as the City column
- On the SlectedIndexChanged of the first one said that the Dropdownlist2.selectedvalue=Dropdownlist1.selectedvalue
-On the SlectedIndexChanged of the second one said that the Dropdownlist1.selectedvalue=Dropdownlist2.selectedvalue
-  Set the Autopostback to true en both dropdown list and is done, good to go
0
 

Author Comment

by:maartendierckxsens
ID: 34211044
Can it also been done on the text of the dropdownlist?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 14

Expert Comment

by:systan
ID: 34211145
hi,  try my tested code and analyze the code shows.
using Sql.Data.SqlClient;

namespace Form1
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        string ConString = "SERVER=localhost;" +
                        "port=SQL_PORT_YOU_ADDRESS;" +
                        "database=YOUR_DATABASE_NAME_HERE;" +
                        "user id=YOUR_USERNAME;" +
                        "password=;";

       SqlConnection con;

       private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            con.Close();
        }


       private void Form1_Load(object sender, EventArgs e)
        {
            con = new SqlConnection(ConString);
            con.Open();
            con.InitializeLifetimeService();

	populate_citylist();
	populate_codeList();

       }
       

 private void populate_codeList()
            {
                string query = "select code from codecity where city='" + ddlBillToCity.SelectedItem + "' ";
                using (SqlCommand ad = new SqlCommand(query, con))
                {
                    SqlDataReader dr = ad.ExecuteReader();
                    if (!dr.HasRows) return; 
                    while (dr.Read())
                    {
                        ddlBillToPostCode.Items.Add(dr.GetValue(0));  
                    }
                    dr.Close();
                    ddlBillToPostCode.SelectedIndex=0; 
               }
            }


 private void populate_citylist()
            {
                string query = "select city from codecity";
                using (SqlCommand ad = new SqlCommand(query, con))
                {
                    SqlDataReader dr = ad.ExecuteReader();
                    if (!dr.HasRows) return; 
                    while (dr.Read())
                    {
                        ddlBillToCitye.Items.Add(dr.GetValue(0));  
                    }
                    dr.Close();
                    ddlBillToCity.SelectedIndex=0; 
               }
            }



protected void ddlBillToCity_SelectedIndexChanged(object sender, EventArgs e)
    {
        populate_codelist();
    }

     
 }
}

Open in new window

0
 
LVL 14

Expert Comment

by:systan
ID: 34211157
Additional; you can set styles upon loading;

       private void Form1_Load(object sender, EventArgs e)
        {
            con = new SqlConnection(ConString);
            con.Open();
            con.InitializeLifetimeService();

      //here it is
      ddlBillToCity.DropDownStyle = ComboBoxStyle.DropDownList;
      ddlBillToPostCode.DropDownStyle = ComboBoxStyle.DropDownList;


      populate_citylist();
      populate_codeList();

       }


0
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 34211162
Yes, it van be done by the text
0
 

Author Comment

by:maartendierckxsens
ID: 34211391
Ok, I have it done with the following:

protected void ddlBillToCity_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlBillToPostCode.SelectedIndex = ddlBillToCity.SelectedIndex;
    }


Thanks to everybody for the support.
0
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 34211398
Be sure That the selectedindex be the same for both dropdown
0
 

Author Closing Comment

by:maartendierckxsens
ID: 34211422
a solution is found, thanks for the support
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question