Cascading DropDown Lists not working

Discussion in '.NET' started by skamranj, Nov 29, 2010.

  1. skamranj

    skamranj

    Joined:
    Nov 29, 2010
    Messages:
    1
    Hi,

    I have 2 DropDownLists and a Button on a simple aspx page. connecting to an oracle database. The 1st DropDownList1 gets filled on page load.

    When I select an item from the DropDownList1 the 2nd DropDownList2 get populated with the related records but the 1st DropDownList1 looses its selected item and moves the selection to the 1st item in the list and changes its selected index value to 1.

    Anyone has any idea, how to keep the selected item retain its selected index value in the DropDownList1?

    here is the code for aspx page.

    Code:
        
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>  
          
        <!DOCTYPE html PUBLIC >  
          
        <html>
        <head runat="server">  
            <title>Untitled Page</title>  
        </head>  
        <body>  
           <form id="form1" runat="server">  
           <div>  
             
               <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"   
                   onselectedindexchanged="DropDownList1_SelectedIndexChanged">  
               </asp:DropDownList>  
               <br />  
               <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"   
                   onselectedindexchanged="DropDownList2_SelectedIndexChanged">  
               </asp:DropDownList>  
               <br />  
               <asp:Button ID="Button1" runat="server" Text="Button" />  
             
           </div>  
           </form>  
       </body>  
       </html>  

    Here is the code for code behind c# page:


    Code:
    public partial class test : System.Web.UI.Page
    {
        string ConnectionString = ConfigurationManager.ConnectionStrings["GSMBillHistoryConnectionString"].ConnectionString;
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string vuser = UserManager.GetUserNetworkID().ToUpper();
    
                string SQLStatementServiceNum = "select * from zeodba.T_zeo_custodian where cust_network_id = '" + vuser + "' order by SIM_NUM";
                using (OracleConnection connection = new OracleConnection(ConnectionString))
                {
                    OracleCommand command = new OracleCommand(SQLStatementServiceNum, connection);
                    connection.Open();
                    OracleDataReader reader = command.ExecuteReader();
                    DropDownList1.DataSource = reader;
                    DropDownList1.DataTextField = "SIM_NUM";
                    DropDownList1.DataValueField = "SIM_VENDOR";
                    DropDownList1.DataBind();
                    DropDownList1.Items.Insert(0, new ListItem("Select SIM Number", "0"));
                    reader.Close();
                    connection.Close();
                }
            }
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedIndex != 0)
            {
                Fill_BillPeriod(DropDownList1.SelectedValue);
            }
            else
            {
                DropDownList2.Items.Clear();
                Button1.Enabled = false;
    
            }
        }
        protected void Fill_BillPeriod(string cSIMVendor)
        {
            string SQLStatementBillPeriod = "select vendor, to_char(bill_date, 'MM/DD/YYYY') as BILL_DATE from zeodba.t_zeo_bill_date where vendor ='" + cSIMVendor + "' Order by BILL_DATE";
            using (OracleConnection connection = new OracleConnection(ConnectionString))
            {
                OracleCommand command = new OracleCommand(SQLStatementBillPeriod, connection);
                connection.Open();
                OracleDataReader reader = command.ExecuteReader();
    
                DropDownList2.DataSource = reader;
                DropDownList2.DataTextField = "BILL_DATE";
                DropDownList2.DataValueField = "BILL_DATE";
                DropDownList2.DataBind();
                DropDownList2.Items.Insert(0, new ListItem("Select Bill Date", "01/01/1900"));
                reader.Close();
                connection.Close();
            }
        }
        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList2.SelectedIndex == 0 || DropDownList2.SelectedIndex < 0)
            {
                Button1.Enabled = false;
                
            }
            else
            {
                Button1.Enabled = true;
            }
        }
    }
    
     
    Last edited: Nov 30, 2010
    skamranj, Nov 29, 2010
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sam
    Replies:
    2
    Views:
    9,495
  2. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    410
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  3. pintu
    Replies:
    0
    Views:
    1,210
    pintu
    Sep 14, 2006
  4. acadam
    Replies:
    0
    Views:
    762
    acadam
    Dec 27, 2006
  5. naag c

    Need Help about Cascading DropDown

    naag c, Oct 16, 2009, in forum: ASP .Net
    Replies:
    1
    Views:
    350
    Alexey Smirnov
    Oct 16, 2009
Loading...

Share This Page