Solved

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

Posted on 2010-11-24
9
607 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
AutoHotkey ActiveX Component Error 8 55
How to Get total minutes between two hours 4 27
Showdialog 8 20
VB.NET HttpWebRequest 12 32
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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.
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

705 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

18 Experts available now in Live!

Get 1:1 Help Now