Solved

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

Posted on 2010-11-24
9
608 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
  • 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

914 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now