Solved

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

Posted on 2010-11-24
9
615 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 125 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
Stressed Out?

Watch some penguins on the livecam!

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

691 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