Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2010-11-24
9
Medium Priority
?
620 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

636 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