Cascading DropDown Lists not working


Joined
Nov 29, 2010
Messages
1
Reaction score
0
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:
Ad

Advertisements


Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top