SelectedIndexChanged only works once

Last post 11-23-2008 9:36 PM by exuviae. 10 replies.

Sort Posts:

  • SelectedIndexChanged only works once

    06-27-2008, 4:23 PM

    I've seen one other post on this, but it was related to using datagrid.  I'm not using a datagrid; I am using labels with the visibility property set to true or false based on what is set in my SelectedIndexChanged method.

    It's a pretty typical setup:  I have country, province and state dropdowns populated with a SQLdataReader.  When the page first loads, "Country" and "State" only are displayed, and both of these dd's are set to "select..."

     Now, I'm having problems with subsequent changes to that country dd.  Basically, the FIRST time I select either "Canada" or "United States, the Province or State dd displays as appropriate.  However, any subsequent selection of Canada or U.S. only displays the country dd, no province OR state. 

    I'm sure I'm missing something completely obvious and I'll feel like a dolt, but can someone take a look and see what I'm doing wrong?  I do have autoeventwireup=true, and the dd is set to autopostback.  Is there something else I need to check in the page_load event now?

    The form fields in question:
    Country
    "country" runat="server" OnSelectedIndexChanged="country_SelectedIndexChanged" AutoPostBack="true">
    "StateRow" runat="server" Visible="true"> State "state" runat="server"> "ProvinceRow" runat="server" Visible="false"> Province  "ProvinceDD" runat="server"> The event: void country_SelectedIndexChanged(object sender, EventArgs e) { if (country.SelectedValue == "USA") { StateRow.Visible = true; ProvinceRow.Visible = false; } else if (country.SelectedValue == "CAN") { StateRow.Visible = false; ProvinceRow.Visible = true; } else { StateRow.Visible = false; ProvinceRow.Visible = false; } }
     
  • Re: SelectedIndexChanged only works once

    06-27-2008, 5:58 PM
    • All-Star
      91,728 point All-Star
    • vinz
    • Member since 10-05-2007, 3:47 PM
    • Cebu, Philippines
    • Posts 13,769
    • TrustedFriends-MVPs

    If you are Binding your DDL manually on Page_Load then be sure to place it inside Not IsPostBack method to get appropriate result when page is posted back

    if (!Page.IsPostBack)

    {

        //bind your DDL 

     

    "Code,Beer and Music ~ my way of being a programmer"

  • Re: SelectedIndexChanged only works once

    06-27-2008, 11:06 PM

    Hi Vincent,

     Thanks for your reply.  I'm actually not binding the data in the page_load event - - they are each a separate method called from within the page_load event, and those calls are in a "if(!IsPostBack)" statement (example of what I'm doing shown below).  I'm doing okay with populating the data, just not getting the fields to display when they should (?).  Thanks for your help.

    if (!IsPostBack)
    {
      //Do some other stuff related to the form
    
      // Load the State and Country DD lists
                LoadStateList();
                LoadCountryList();
                LoadProvinceList();   
    }
    
    And an example of the methods for binding the data:
    
    void LoadStateList()
        {
            
              //// Get the list of states           
            
            SqlConnection conn = null;
            conn = new SqlConnection(myConnectionStr);
            SqlCommand myCommand;
            myCommand = new SqlCommand("USP_GET_STATES", conn);
            myCommand.CommandType = CommandType.StoredProcedure;
    
            myCommand.Connection.Open();
            SqlDataReader myReader = myCommand.ExecuteReader();
    
            ListItem aItem = new ListItem("Select", "");
            state.Items.Insert(0, aItem);
    
            int idx = 1;
            while (myReader.Read())
            {
                string state_code = (string)myReader["state_code"];
                string state_desc = (string)myReader["state_desc"];
                aItem = new ListItem(state_desc, state_code);
                state.Items.Insert(idx, aItem);
                idx++;
            }
            myReader.Close();
            myCommand.Connection.Close();
        }
        
    
     
  • Re: SelectedIndexChanged only works once

    06-30-2008, 9:02 AM
    • All-Star
      91,728 point All-Star
    • vinz
    • Member since 10-05-2007, 3:47 PM
    • Cebu, Philippines
    • Posts 13,769
    • TrustedFriends-MVPs

    Hi LordOfTheBathtubRings,

    Have you tried to debug your codes by setting a breakpoint in the country_SelectedIndexChanged just to check if what's going on there? Step to your codes and check your codition if it really hits there.. Also try to use SelectedItem.Text for comparing values instead of SelectedValue like below

    void country_SelectedIndexChanged(object sender, EventArgs e)
    {
    if (country.SelectedItem.Text == "USA")
    {
    StateRow.Visible = true;
    ProvinceRow.Visible = false;
    }
    else if (country.SelectedItem.Text == "CAN")
    {
    StateRow.Visible = false;
    ProvinceRow.Visible = true;
    }
    else
    {
    StateRow.Visible = false;
    ProvinceRow.Visible = false;
    }
    }

     

    "Code,Beer and Music ~ my way of being a programmer"

  • Re: SelectedIndexChanged only works once

    07-28-2008, 2:16 PM

    Hi Vinz,

     Sorry it took me so long to respond...got pulled onto a different project and I'm just getting back to this.

    I tried your suggestion but it still isn't working, and VWD isn't letting me set a breakpoint...

    SelectedItem.Text caused it to make the State field disappear entirely, regardless of what is selected.

  • Re: SelectedIndexChanged only works once

    11-23-2008, 1:00 AM

    Still having this problem.  I did step into the code and it hits the onselectedchanged event the first time around, but not subsequent changes.

     Per some other posts I found, I did make sure that AutoPostback = true and EnableViewState=true.  Per another thread I also tried hard coding a listitem into my dropdown, but that didn't work, either.

     This is driving me crazy. What am I doing wrong?

  • Re: SelectedIndexChanged only works once

    11-23-2008, 2:39 AM
    • Participant
      1,165 point Participant
    • exuviae
    • Member since 11-21-2008, 11:23 PM
    • LA
    • Posts 220

    I thought this behaviour would be interesting to investigate, but for the last hour i have been unable to replicate a scenario where SelectedIndexChanged only gets called once.  Can you post the complete aspx you are using, so I can investigate a little further.


    Chris Dupuy

    Sr. Developer

    Blue Cross/Blue Shield LA



    * Please remember to click Mark as Answer on this post if it helped you *
  • Re: SelectedIndexChanged only works once

    11-23-2008, 3:08 AM

    Sure, Chris.  Thanks for your help...

     

    protected void country_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (country.SelectedItem.Value == "USA")
            {
                StateRow.Visible = true;
                ProvinceRow.Visible = false;
                
            }
            else if (country.SelectedItem.Value == "CAN")
            {
                StateRow.Visible = false;
                ProvinceRow.Visible = true;
                
            }
            else 
            {
                StateRow.Visible = false;
                ProvinceRow.Visible = false;
            }
            
        }
    
    private void LoadStateList()
        {
            
            //// Get the list of states           
            
            SqlConnection conn = null;
            conn = new SqlConnection(myConnectionStr);
            SqlCommand myCommand;
            myCommand = new SqlCommand("USP_GET_STATES", conn);
            myCommand.CommandType = CommandType.StoredProcedure;
    
            myCommand.Connection.Open();
            SqlDataReader myReader = myCommand.ExecuteReader();
    
            ListItem aItem = new ListItem("Select", "");
            state.Items.Insert(0, aItem);
    
            int idx = 1;
            while (myReader.Read())
            {
                string state_code = (string)myReader["state_code"];
                string state_desc = (string)myReader["state_desc"];
                aItem = new ListItem(state_desc, state_code);
                state.Items.Insert(idx, aItem);
                idx++;
            }
            myReader.Close();
            myCommand.Connection.Close();
        }
        
        private void LoadCountryList()
        {
            
             //// Get the list of Countries           
            
            SqlConnection conn = null;
            conn = new SqlConnection(myConnectionStr);
            SqlCommand myCommand;
            myCommand = new SqlCommand("USP_GET_COUNTRIES", conn);
            myCommand.CommandType = CommandType.StoredProcedure;
    
            myCommand.Connection.Open();
            SqlDataReader myReader = myCommand.ExecuteReader();
    
            ListItem aItem = new ListItem("Select", "");
            country.Items.Insert(0, aItem);
    
            int idx = 1;
            while (myReader.Read())
            {
                string country_code = (string)myReader["country_code"];
                string country_desc = (string)myReader["country_desc"];
                aItem = new ListItem(country_desc, country_code);
                country.Items.Insert(idx, aItem);
                idx++;
            }
            myReader.Close();
            myCommand.Connection.Close();
        }
        
        private void LoadProvinceList()
        {
            ListItem aItem = new ListItem("Select", "");
            ProvinceDD.Items.Insert(0, aItem);
    
            int idx = 1;
            for (int i=0; istring pro_name = provinceA[i];
                string pro_value = provinceCodeA[i];
    
                aItem = new ListItem(pro_name, pro_value);
                ProvinceDD.Items.Insert(idx, aItem);
                idx++;
            }
        }
     
                 
        private void Page_Load(object sender, System.EventArgs e)
        
       
        {
        
           //do some stuff first...        
    
            if (!IsPostBack)
            {
                ShippingForm.Visible = true;
                Confirm.Visible = false;
                            
                // Load the State and Country DD lists
                LoadStateList();
                LoadCountryList();
                LoadProvinceList(); 
                
               // Do other stuff...
    	   }
          }
    
    
    Country:
              <asp:UpdatePanel  
    ID="UpdatePanel2"
    runat="server">        
    <ContentTemplate>

    <asp:DropDownList
    ID="country"
    runat="server"
    OnSelectedIndexChanged="country_SelectedIndexChanged" 
    EnableViewState="true"
    AutoPostBack="true">
    </asp:DropDownList><br />                   
    	<asp:Label
    ID="StateRow"
    runat="server"
    Visible="true"
    EnableViewState="true">
    State:           
    <asp:DropDownList
    ID="state"            
    runat="server"
    > 
    </
    asp:DropDownList>           
    </asp:Label>
                       
    	<asp:Label             
    ID="ProvinceRow"
    runat="server"
    Visible="false"
    EnableViewState="true"           
    >
               
    Province:
               
    <asp:DropDownList
               
    ID="ProvinceDD"
                
    runat="server">
               
    </asp:DropDownList>
               
    </asp:Label>
                   
    </ContentTemplate>
    </asp:UpdatePanel>
  • Re: SelectedIndexChanged only works once

    11-23-2008, 1:22 PM
    Answer
    • Participant
      1,165 point Participant
    • exuviae
    • Member since 11-21-2008, 11:23 PM
    • LA
    • Posts 220

    Ok I took your code and had a look, and was able to see what was happening.  It was the one thing I hadn't tried, a dropdown list inside a label.  I changed the aspx to produce the same result I think you are looking for.  First i put the state label and dd in a panel set to visible, then I put the province lable and dd in another panel set to visible="false".  In your OnSelectedIndexChanged you will want to check .Text instead of .Value because you are setting value to country_code which will not be the "USA" or "CAN" the user sees and selects.  Here are the results:

     

         <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                <ContentTemplate>
                    <asp:DropDownList ID="country" runat="server" OnSelectedIndexChanged="country_SelectedIndexChanged"
                        EnableViewState="true" AutoPostBack="true">
                    </asp:DropDownList>
                    <br />
                    <asp:Panel ID="StatePanel" runat="server">
                        <asp:Label ID="StateLbl" runat="server" Visible="true" Text="State:" EnableViewState="true" />
                        <asp:DropDownList ID="state" runat="server" Visible="true">
                        </asp:DropDownList>
                    </asp:Panel>
                    <asp:Panel ID="ProvincePanel" runat="server" Visible="false">
                        <asp:Label ID="ProvinceLbl" runat="server" Visible="true" Text="Province:" EnableViewState="true" />
                        <asp:DropDownList ID="ProvinceDD" runat="server" Visible="true">
                        </asp:DropDownList>
                    </asp:Panel>
                </ContentTemplate>
            </asp:UpdatePanel>
      
    protected void country_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (country.SelectedItem.Text == "USA")
            {
                StatePanel.Visible = true;
                ProvincePanel.Visible = false;
    
            }
            else if (country.SelectedItem.Text == "CAN")
            {
                StatePanel.Visible = false;
                ProvincePanel.Visible = true;
    
            }
            else
            {
                StatePanel.Visible = false;
                ProvincePanel.Visible = false;
            }
        }
     

    Chris Dupuy

    Sr. Developer

    Blue Cross/Blue Shield LA



    * Please remember to click Mark as Answer on this post if it helped you *
  • Re: SelectedIndexChanged only works once

    11-23-2008, 9:07 PM

    Chris, thank you so much!  It's perfect!

    I did end up having to leave it as SelectedItem.Value instead of .Text, because the country_codes are "CAN", "USA", etc. and the text is "Canada", "United States", etc.

     Thank you! Yes  In the span of 24 hours you have resolved two issues that have been plaguing me for quite some time now.  I may have to throw my one final .net hurdle at you so I can launch this thing this week.  :D

    Terri

  • Re: SelectedIndexChanged only works once

    11-23-2008, 9:36 PM
    • Participant
      1,165 point Participant
    • exuviae
    • Member since 11-21-2008, 11:23 PM
    • LA
    • Posts 220

    Glad I could help.  If i fail to catch your post, feel free to send me a private message with the problem.


    Chris Dupuy

    Sr. Developer

    Blue Cross/Blue Shield LA



    * Please remember to click Mark as Answer on this post if it helped you *
Page 1 of 1 (11 items)