RadComboBox interact with other RadComboBox but selectedvalue is empty

5 posts, 1 answers
  1. jeslopmay
    jeslopmay avatar
    10 posts
    Member since:
    Feb 2012

    Posted 24 Feb 2012 Link to this post

    Hello,

    I have several RadComboBoxes that interact with each other using client-side methods and requesting the items on demand, like here: http://demos.telerik.com/aspnet-ajax/combobox/examples/functionality/multiplecomboboxes/defaultcs.aspx
    This works well, but when I need the SelectedValue it's always empty.

    Can anyone help me please? (I don't know what I'm doing wrong...)

    Here I put the code used:

    RadComboBoxes
    <telerik:RadComboBox ID="cboProvinciaT" runat="server" Width="200px" OnClientSelectedIndexChanging="LoadLocalidades" OnItemsRequested="cboProvinciaT_ItemsRequested" MarkFirstMatch="true" Skin="Sunset"></telerik:RadComboBox>
    <telerik:RadComboBox ID="cboLocalidadT" runat="server" Width="200px" OnClientSelectedIndexChanging="LoadTiposVia" OnClientItemsRequested="ItemsLoaded" OnItemsRequested="cboLocalidadT_ItemsRequested" MarkFirstMatch="true" Skin="Sunset"></telerik:RadComboBox>
    <telerik:RadComboBox ID="cboTipoViaT" runat="server" Width="80px" OnClientSelectedIndexChanged="LoadNombresVia" OnClientItemsRequested="ItemsLoaded" OnItemsRequested="cboTipoViaT_ItemsRequested" MarkFirstMatch="true" Skin="Sunset"></telerik:RadComboBox>
    <telerik:RadComboBox ID="cboNombreVia" runat="server" OnClientItemsRequested="ItemsLoaded" OnItemsRequested="cboNombreVia_ItemsRequested" Filter="Contains" Skin="Sunset"></telerik:RadComboBox>

    Scripts
    <script type="text/javascript">
        var LocalidadesCombo;
        var TiposViaCombo;
        var NombresVia;
     
        function pageLoad() {
            LocalidadesCombo = $find("<%= cboLocalidadT.ClientID %>");
            TiposViaCombo = $find("<%= cboTipoViaT.ClientID %>");
            NombresVia = $find("<%= cboNombreVia.ClientID %>");
        }
     
        function LoadLocalidades(sender, eventArgs) {
            var item = eventArgs.get_item();
            LocalidadesCombo.set_text("Cargando...");
            TiposViaCombo.clearSelection();
     
            if (item.get_index() > 0) {
                LocalidadesCombo.requestItems(item.get_value(), false);
                LocalidadesCombo.clearSelection();
            }
            else {
                LocalidadesCombo.set_text(" ");
                LocalidadesCombo.clearItems();
     
                TiposViaCombo.set_text(" ");
                TiposViaCombo.clearItems();
            }
        }
     
        function LoadTiposVia(sender, eventArgs) {
            var item = eventArgs.get_item();
     
            TiposViaCombo.set_text("Cargando...");
            TiposViaCombo.requestItems(item.get_value(), false);
            TiposViaCombo.clearSelection();
     
            if (item.get_index() > 0) {
                TiposViaCombo.requestItems(item.get_value(), false);
                TiposViaCombo.clearSelection();
            }
            else {
                TiposViaCombo.set_text(" ");
                TiposViaCombo.clearItems();
                NombresVia.set_text(" ");
                NombresVia.clearItems();
            }
     
        }
     
        function LoadNombresVia(sender, eventArgs) {
            var item = eventArgs.get_item();
            NombresVia.set_text("Cargando...");
            NombresVia.requestItems(item.get_value(), false);
            NombresVia.clearSelection();
        }
     
        function ItemsLoaded(sender, eventArgs) {
           sender.showDropDown();
        }
    </script>

    Page_Load (server-side)
    if (!Page.IsPostBack)
    {
        CargarProvincias(); //This method fill cboProvinciaT
    }
    else if (!Page.IsCallback)
    {
        LoadLocalidades(cboProvinciaT.SelectedValue);
        LoadTiposVia(cboLocalidadT.SelectedValue);
        LoadNombresVia(cboTipoViaT.SelectedValue);
    }

    Rest of functions
    #region Dirección
     
    protected void LoadLocalidades(string pIdProvincia)
    {
     
        try
        {
            AD.Motor mMotorAD = new AD.Motor();
     
            DataTable mDT = new DataTable();
     
            mDT = mMotorAD.ExecuteDataTable("Select 0 as ID, 'Localidad...' as Nombre union SELECT ID, Nombre FROM tlLocalidadDN where idProvincia=" + pIdProvincia + " ORDER BY Nombre ASC");
     
            DataSet mDS = new DataSet();
            mDS.Tables.Add(mDT);
     
            cboLocalidadT.DataTextField = "Nombre";
            cboLocalidadT.DataValueField = "ID";
            cboLocalidadT.DataSource = mDS.Tables[0];
            cboLocalidadT.DataBind();
             
     
     
        }
        catch (Exception ex)
        {
     
            throw ex;
        }
         
    }
    protected void LoadTiposVia(string pIdLocalidad)
    {
        try
        {
            AD.Motor mMotorAD = new AD.Motor();
     
            DataTable mDT = new DataTable();
     
            mDT = mMotorAD.ExecuteDataTable("Select 0 as ID, 'Vía..' as Nombre union SELECT ID, Nombre FROM tlTipoViaDN ORDER BY Nombre ASC");
     
            DataSet mDS = new DataSet();
            mDS.Tables.Add(mDT);
     
            cboTipoViaT.DataTextField = "Nombre";
            cboTipoViaT.DataValueField = "ID";
            cboTipoViaT.DataSource = mDS.Tables[0];
            cboTipoViaT.DataBind();
     
     
        }
        catch (Exception ex)
        {
     
            throw ex;
        }
    }
    protected void LoadNombresVia(string pIdTipoVia)
    {
        try
        {
     
            //HERE ALWAYS GET empty ("")
               string
    mIdLocalidad = cboLocalidadT.SelectedValue;
            AD.Motor mMotorAD = new AD.Motor();
     
            DataTable mDT = new DataTable();
     
            mDT = mMotorAD.ExecuteDataTable("Select 0 as ID, '  Nombre de la Vía..' as Nombre union SELECT ID, Nombre FROM tlViaDN where idLocalidad=" + mIdLocalidad + " and idTipovia=" + pIdTipoVia + " order by Nombre");
     
            DataSet mDS = new DataSet();
            mDS.Tables.Add(mDT);
     
            cboNombreVia.DataTextField = "Nombre";
            cboNombreVia.DataValueField = "ID";
            cboNombreVia.DataSource = mDS.Tables[0];
            cboNombreVia.DataBind();
     
        }
        catch (Exception ex)
        {
             
            throw ex;
        }
    }
    protected void LoadNombresVia(string pIdTipoVia, string pIdLocalidad)
    {
        try
        {
     
            AD.Motor mMotorAD = new AD.Motor();
     
            DataTable mDT = new DataTable();
     
            mDT = mMotorAD.ExecuteDataTable("Select 0 as ID, '  Nombre de la Vía..' as Nombre union SELECT ID, Nombre FROM tlViaDN where idLocalidad=" + pIdLocalidad + " and idTipovia=" + pIdTipoVia + " order by Nombre");
     
            DataSet mDS = new DataSet();
            mDS.Tables.Add(mDT);
     
            cboNombreVia.DataTextField = "Nombre";
            cboNombreVia.DataValueField = "ID";
            cboNombreVia.DataSource = mDS.Tables[0];
            cboNombreVia.DataBind();
     
        }
        catch (Exception ex)
        {
     
            throw ex;
        }
    }
    protected void cboNombreVia_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
    {
        LoadNombresVia(e.Text);
    }
    protected void cboProvinciaT_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
    {
        CargarProvincias();
    }
    protected void cboLocalidadT_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
    {
        LoadLocalidades(e.Text);
     
    }
    protected void cboTipoViaT_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
    {
        LoadTiposVia(e.Text);
    }
     
    #endregion

    When I try to get cboLocalidadT.SelectedValue in function LoadNombresVia
    protected void LoadNombresVia(string pIdTipoVia)
            {
                try
                {
                    //********************************************
                    //**** HERE ALWAYS GET EMPTY VALUE --> "" ****
                    //********************************************
     
                    string mIdLocalidad = cboLocalidadT.Text;
     
                    //********************************************
     
                    AD.Motor mMotorAD = new AD.Motor();
     
                    DataTable mDT = new DataTable();
     
                    mDT = mMotorAD.ExecuteDataTable("Select 0 as ID, '  Nombre de la Vía..' as Nombre union SELECT ID, Nombre FROM tlViaDN where idLocalidad=" + mIdLocalidad + " and idTipovia=" + pIdTipoVia + " order by Nombre");
     
                    DataSet mDS = new DataSet();
                    mDS.Tables.Add(mDT);
     
                    cboNombreVia.DataTextField = "Nombre";
                    cboNombreVia.DataValueField = "ID";
                    cboNombreVia.DataSource = mDS.Tables[0];
                    cboNombreVia.DataBind();
     
                }
                catch (Exception ex)
                {
                     
                    throw ex;
                }
            }

    Thank you very much for your help!!
  2. jeslopmay
    jeslopmay avatar
    10 posts
    Member since:
    Feb 2012

    Posted 27 Feb 2012 Link to this post

    I would appreciate any help or guidance you could give me, please.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. msigman
    msigman avatar
    141 posts
    Member since:
    Apr 2008

    Posted 27 Feb 2012 Link to this post

    Instead of 
    string mIdLocalidad = cboLocalidadT.Text;

    try

    string mIdLocalidad = cboLocalidadT.SelectedItem.Text;

  5. Answer
    Dimitar Terziev
    Admin
    Dimitar Terziev avatar
    1266 posts

    Posted 28 Feb 2012 Link to this post

    Hello Guys,

    The reason for the experienced behavior is the following. When the ItemsRequested event is fired a callback to the server is made and not a regular post-back. The important part in this case is the fact that the current state of the controls on the page is not send to the server and therefore you could not get the SelectedValue of your second combobox. In the event handler function you could only access the data from the event arguments. If you want to pass the selected value of the second combobox you should add it to the context object as show in the following help article here.

    Greetings,
    Dimitar Terziev
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  6. jeslopmay
    jeslopmay avatar
    10 posts
    Member since:
    Feb 2012

    Posted 28 Feb 2012 Link to this post

    Thank you SO much to both!

    At finally worked fine using the context object.


    Adds to my code to show it running:

    RadComboBox --> cboNombreVia
    Add this property:
    OnClientItemsRequesting="GetSelectedItem"

    new function
    function GetSelectedItem(sender, eventArgs) {
           var LocalidadCombo = $find("<%= cboLocalidadT.ClientID %>");
           var LocalidadText = LocalidadCombo.get_text();
           var context = eventArgs.get_context();
           context["Localidad"] = LocalidadText;
       }



    Thank you very much again for answering my question.

    Greetings,
    Jeslopmay
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017