This is a migrated thread and some comments may be shown as answers.

cascading dropdown returning wrong value.

4 Answers 154 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Kris
Top achievements
Rank 1
Kris asked on 23 Jan 2011, 01:22 AM
The problem that I am having is with the cascading drop down combo box.

When you preset the first, and second drop down with default values or preset values like you would have if a user has to re-edit a form.  The second drop drop down will not return the selected value if the user changes the first drop down then selects the second  drop down.  The value returned is always the value of the first item in the drop-down list.

Example:  preset the a country drop down to United States. Then the second to Florida.  If the user changes the country to Canada then selects a Canadian region, the value returned will be the first state listed for the United States not the Canadian region selected.  

.ascx code
<tr>
                <td colspan="2" class="t12_BlackBold"><div align="right">
                  Country:  <span class="t12_red">*</span>
                </div></td>
                <td class="style1"><div align="left">
                    <telerik:RadComboBox ID="frm_sCountry_cd" Runat="server" 
                        DataTextField="sCountry" 
                        DataValueField="sCountry_cd" 
                                                 
                        ErrorMessage="Required field." 
                        Skin="Outlook" 
                        Width="180px" 
                        DropDownWidth="240px" 
                        MaxHeight="300px"
                        onclientselectedindexchanged="LoadStates"
                        OnItemsRequested="frm_sCountry_cd_ItemsRequested">
                    </telerik:RadComboBox><asp:CompareValidator ID="sCountry_cd_VD" runat="server"
                        ValueToCompare="-Select a Country-"
                        Operator="NotEqual"
                        ControlToValidate="frm_sCountry_cd"
                        CssClass="KT_field_error" Font-Bold="True" Display="Dynamic"
                        ErrorMessage="<br />You must select a country."/>
                    </div></td>
              </tr>
              <tr>
                <td colspan="2" class="t12_BlackBold"><div align="right">
                  State/Region: <span class="t12_red">*</span>
                </div></td>
                <td class="style1"><div align="left">
                    <telerik:RadComboBox ID="frm_sState" Runat="server" 
                        DataTextField="sStateDesc" DataValueField="sState" 
                        EmptyMessage="-Select State-" 
                        ErrorMessage="Required field." 
                        Skin="Outlook" Width="180px" 
                        MaxHeight="300px"
                        DropDownWidth="200px"
                        OnClientItemsRequested="ItemsLoaded"
                        OnItemsRequested="frm_sState_ItemsRequested" >
                    </telerik:RadComboBox><asp:CompareValidator ID="sState_VD" runat="server"
                        ValueToCompare="-Select State/Region-"
                        Operator="NotEqual"
                        ControlToValidate="frm_sCountry_cd"
                        CssClass="KT_field_error" Font-Bold="True" Display="Dynamic"
                        ErrorMessage="<br />You must select a state/region."/>
                    </div></td>
              </tr>
  
.
.
.
.
<script type="text/javascript">
//global variables for the countries and cities comboboxes
var sStatesCombo;
  
function pageLoad()
{
    // initialize the global variables
    // in this event all client objects 
    // are already created and initialized 
    sStatesCombo = $find("<%= frm_sState.ClientID%>");
  
function LoadStates(combo, eventArqs)
{
    var item = eventArqs.get_item();
    sStatesCombo.set_text("Loading...");
    // if a continent is selected
    if (item.get_index() > 0)
    {        
     // this will fire the ItemsRequested event of the 
     // countries combobox passing the sCountry_id as a parameter 
          
        if (sStatesCombo.get_visible == false ) 
        {
            sStatesCombo.set_visible(true);
        }
        sStatesCombo.clearItems();
        sStatesCombo.requestItems(item.get_value(), false);
          
        sStatesCombo.commitChanges();
           
        items = sStatesCombo.get_items();
        if (items.get_count() == 0)
        {
            sStatesCombo.set_text("-Select State/Region-");
        }
        else 
        {
            sStatesCombo.clearItems();
        }
    }
    else
    {
     // the -Select a continent- item was chosen
        sStatesCombo.set_text("-Select Country first-");
        sStatesCombo.clearItems();
    }
}
  
function ItemsLoaded(combo, eventArqs) {
    if (combo.get_items().get_count() > 0) {
        // pre-select the first item
        combo.set_text(combo.get_items().getItem(0).get_text());
        combo.get_items().getItem(0).highlight();
    }
    combo.showDropDown();
}
 </script>


.ascx.vb code
'Code used in updating the values
...
 'Setup Country and state drop downs 
                LoadCountries()
            frm_sCountry_cd.SelectedValue = sCountry_cd
            LoadStates(sCountry_cd)
            If sState <> "" Then frm_sState.SelectedValue = sState
...
  
Protected Sub LoadCountries()
        Dim connection As New SqlConnection(ConfigurationManager.ConnectionStrings("AAU_StoreCS").ConnectionString)
        Dim adapter As New SqlDataAdapter("SELECT Value AS sCountry_cd, Text AS sCountry, EntryID AS ParentID, 0 as sOrder FROM Mstr_Lists WHERE (ListName = 'Country') AND (Value = 'US') UNION SELECT Value AS sCountry_cd, Text AS sCountry, EntryID AS ParentID, 1 as sOrder FROM Mstr_Lists AS Mstr_Lists_1 WHERE (ListName = 'Country') Order by sOrder, sCountry", connection)
        Dim dt As New DataTable()
        adapter.Fill(dt)
  
        frm_sCountry_cd.DataTextField = "sCountry"
        frm_sCountry_cd.DataValueField = "sCountry_cd"
        frm_sCountry_cd.DataSource = dt
        frm_sCountry_cd.DataBind()
        'insert the first item
        frm_sCountry_cd.Items.Insert(0, New RadComboBoxItem("-Select a Country-"))
  
    End Sub
  
 Protected Sub LoadStates(ByVal sCountry_cd As String)
        Dim connection As New SqlConnection(ConfigurationManager.ConnectionStrings("AAU_StoreCS").ConnectionString)
  
        'Select states/regions based on the sCountry_cd
        Dim adapter As New SqlDataAdapter("SELECT Value AS sState, Text AS sStateDesc, ParentID FROM Mstr_Lists WHERE (ListName = 'Region') AND (ParentID = (SELECT EntryID FROM Mstr_Lists AS Mstr_Lists_1 WHERE (ListName = 'Country') AND (Value = @Country_cd))) ORDER BY sStateDesc", connection)
  
        adapter.SelectCommand.Parameters.AddWithValue("@Country_cd", sCountry_cd)
  
        Dim dt As New DataTable()
        adapter.Fill(dt)
        frm_sState.DataTextField = "sStateDesc"
        frm_sState.DataValueField = "sState"
        frm_sState.DataSource = dt
        frm_sState.DataBind()
        If Not frm_sState.IsEmpty Then
            frm_sState.Visible = True
            'sStateTxt.Visible = False
            'insert the first item
            If sCountry_cd = "US" Then
                frm_sState.Items.Insert(0, New RadComboBoxItem("-Select State-"))
            Else
                frm_sState.Items.Insert(0, New RadComboBoxItem("-Select State/Region-"))
            End If
        Else
            'frm_sState.Visible = False
            'sStateTxt.Visible = True
        End If
    End Sub
  
  
'Code used to read values during post back.
...
Dim sState As String = Left(Replace(frm_sState.SelectedValue.ToString(), "'", "''"), 20)
Dim sCountry_cd As String = Left(Replace(frm_sCountry_cd.SelectedValue.ToString, "'", ""), 2)
...

4 Answers, 1 is accepted

Sort by
0
Kris
Top achievements
Rank 1
answered on 24 Jan 2011, 07:01 PM
I was hopping someone could help figure out what or why the wrong value is returned.

Thanks for your help.
0
Kris
Top achievements
Rank 1
answered on 25 Jan 2011, 07:25 PM
Looks like I needed to add the following to get the cascading dropdown to work, ".EnabledLoadOnDemand = true."
 If there is a better way please advise.
'Setup Country and state drop downs 
                LoadCountries()
            frm_sCountry_cd.SelectedValue = sCountry_cd 
            frm_sCountry_cd.EnableLoadOnDemand = true      'add this line
            LoadStates(sCountry_cd) 
            If sState <> "" Then
                   frm_sState.SelectedValue = sState 
                   frm_sState.EnableLoadOnDemand = true  'add this line
            end if




0
Kris
Top achievements
Rank 1
answered on 25 Jan 2011, 08:05 PM
The above solution worked in are inial testing but if a user starts to type the state or country instead of selecting we get a loop "wait loading message".

So still looking for a solution.
0
Kalina
Telerik team
answered on 27 Jan 2011, 04:20 PM
Hi Kris,

I am afraid that the code snippets provided illustrate your implementation only partially.
In order to help you we will need more detailed information.
Could you please open a support ticket and send us a simplified runnable page that reproduces the issue?

Regards,
Kalina
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
ComboBox
Asked by
Kris
Top achievements
Rank 1
Answers by
Kris
Top achievements
Rank 1
Kalina
Telerik team
Share this question
or