?
Solved

dropdownlist populates each time upon postback

Posted on 2011-04-21
2
Medium Priority
?
307 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;

}


    }




   



   
      }  
----------

0
Comment
Question by:Codeaddict7423
2 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 35442652
You have if (!IsPostback) in your page_load, but you haven't made it into a code block, so only the first line after the if is basing it's code on the decision. You probably want something more like:
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();
            }
        }
}

Open in new window

0
 

Author Closing Comment

by:Codeaddict7423
ID: 35442856
Thank You. That worked great.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

829 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