We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

dropdownlist populates each time upon postback

Codeaddict7423
on
Medium Priority
333 Views
Last Modified: 2012-05-11
Hello,
I have a page with three dropdownlist controls. the first ddl control displays buildings, the second ddl control displays floors from a building selected, the third ddl control displays positions on that floor.
However, each time any one of the ddl controls is clicked, the page reolads and the first ddl displays repeated choices for buildings.

ANY help would be greatly appreciated.

My code follows:

-----------------
<table style="width:99%;" cellpadding="0" cellspacing="0" border="1">
         
          <tr>
    <td width="25%" valign="top" align="left">
  <br />
  <br />
           
            </td>
            <td width="75%" valign="top" align="left">
             <!--ddl controls below displays choices for jails, floors and positions selected  -->
 
            <br />
           
            <table width="75%" cellpadding="0" cellspacing="1" border="1">
           
            <tr>
            <td width="25%" valign="top" align="left">
                <span style ="font-family:Arial">Select Building : </span>  </td>
            <td width="75%" valign="top" align="left">
            <asp:DropDownList ID="ddl_Building" runat="server" AutoPostBack = "True"
                    OnSelectedIndexChanged="ddl_Building_SelectedIndexChanged"><asp:ListItem Text = "--Select Building--"></asp:ListItem>
</asp:DropDownList>





 </td>
         
            </tr>
           
               <tr>
            <td width="25%" valign="top" align="left">
                <span style ="font-family:Arial">Select Floor : </span>  </td>
            <td width="75%" valign="top" align="left">
            <asp:DropDownList ID="ddl_floor" runat="server" AutoPostBack = "True"
                    Enabled = "False"  OnSelectedIndexChanged="ddl_floor_SelectedIndexChanged">
                <asp:ListItem Text = "--Select Floor--"></asp:ListItem>
</asp:DropDownList>





 </td>
         
            </tr>
           
               <tr>
            <td width="25%" valign="top" align="left">
                <span style ="font-family:Arial">Select Position : </span>  </td>
            <td width="75%" valign="top" align="left">
            <asp:DropDownList ID="ddl_position" runat="server" AutoPostBack = "True"
                    Enabled = "False" OnSelectedIndexChanged="ddl_position_SelectedIndexChanged"><asp:ListItem Text = "--Select Position--"></asp:ListItem>
</asp:DropDownList>





 </td>
         
            </tr>
           
            <tr>
            <td width="25%" valign="top" align="left">
                <span style ="font-family:Arial">Your Choices Are : </span>  </td>
            <td width="75%" valign="top" align="left">
            <asp:Panel ID="pnl_jailsearch" Width="100%" runat="server" BackColor="Gainsboro" Font-Names="Arial" > 
            <asp:Label ID="lblResults" runat="server" Font-Names = "Arial" />





<br /></asp:Panel>






           </td>
         
            </tr>
           
           
            </table>
       -----------

My codebehind is as follows:
--------
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


namespace Detention_DWL
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)

       
           
            pnl_jailsearch.Visible = true;

           

            ddl_Building.AppendDataBoundItems = true;

            String strConnString = ConfigurationManager.ConnectionStrings["DetentionDWLConnectionString"].ConnectionString;

            String strQuery = "select Building_ID, Building_Name from tbl_Building";

            SqlConnection con = new SqlConnection(strConnString);

            SqlCommand cmd = new SqlCommand();

            cmd.CommandType = CommandType.Text;

            cmd.CommandText = strQuery;

            cmd.Connection = con;

            try
            {

                con.Open();

                ddl_Building.DataSource = cmd.ExecuteReader();

                ddl_Building.DataTextField = "Building_Name";

                ddl_Building.DataValueField = "Building_ID";

                ddl_Building.DataBind();

            }



            catch (Exception ex)
            {

                throw ex;

            }

            finally
            {

                con.Close();

                con.Dispose();


            }

   


        }

   
       
         protected void ddl_Building_SelectedIndexChanged(object sender, EventArgs e)

{

    ddl_floor.Items.Clear();

    ddl_floor.Items.Add(new ListItem("--Select Floor--", ""));

    ddl_position.Items.Clear();

    ddl_position.Items.Add(new ListItem("--Select Position--", ""));    

    ddl_floor.AppendDataBoundItems = true;

    String strConnString = ConfigurationManager.ConnectionStrings["DetentionDWLConnectionString"].ConnectionString;

    String strQuery = "select Floor_ID, Floor_Name from tbl_Floor " + "where Building_ID=@Building_ID";

    SqlConnection con = new SqlConnection(strConnString);

    SqlCommand cmd = new SqlCommand();

    cmd.Parameters.AddWithValue("@Building_ID", ddl_Building.SelectedItem.Value);  

    cmd.CommandType = CommandType.Text;

    cmd.CommandText = strQuery;

    cmd.Connection = con;

    try

    {

        con.Open();

        ddl_floor.DataSource = cmd.ExecuteReader();

        ddl_floor.DataTextField = "Floor_Name";

        ddl_floor.DataValueField = "Floor_ID";

        ddl_floor.DataBind();

        if (ddl_floor.Items.Count > 1)

        {

            ddl_floor.Enabled = true;

        }

        else

        {

            ddl_floor.Enabled = false;

            ddl_position.Enabled = false;

        }

    }

    catch (Exception ex)

    {

        throw ex;

    }

    finally

    {

        con.Close();

        con.Dispose();

    }

}



        protected void ddl_floor_SelectedIndexChanged(object sender, EventArgs e)

{

    ddl_position.Items.Clear();

    ddl_position.Items.Add(new ListItem("--Select Position--", ""));

    ddl_position.AppendDataBoundItems = true;

    String strConnString = ConfigurationManager.ConnectionStrings["DetentionDWLConnectionString"].ConnectionString;

    String strQuery = "select POS_ID, POS_Name from tbl_Position " + "where Floor_ID=@Floor_ID";

    SqlConnection con = new SqlConnection(strConnString);

    SqlCommand cmd = new SqlCommand();

    cmd.Parameters.AddWithValue("@Floor_ID", ddl_floor.SelectedItem.Value);

    cmd.CommandType = CommandType.Text;

    cmd.CommandText = strQuery;

    cmd.Connection = con;

    try

    {

        con.Open();

        ddl_position.DataSource = cmd.ExecuteReader();

        ddl_position.DataTextField = "POS_Name";

        ddl_position.DataValueField = "POS_ID";

        ddl_position.DataBind();

        if (ddl_position.Items.Count > 1)

        {

            ddl_position.Enabled = true;

        }

        else

        {

            ddl_position.Enabled = false;

        }

     

    }

    catch (Exception ex)

    {

        throw ex;

    }

    finally

    {

        con.Close();

        con.Dispose();

    }

}





        protected void ddl_position_SelectedIndexChanged(object sender, EventArgs e)

{

    lblResults.Text = " " +

                      ddl_Building.SelectedItem.Text + " -----> " +

                      ddl_floor.SelectedItem.Text + " -----> " +

                      ddl_position.SelectedItem.Text;

}


    }




   



   
      }  
----------

Comment
Watch Question

Senior Systems and Integration Developer
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thank You. That worked great.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.