Changing comboBoxItem's text from client side

5 posts, 0 answers
  1. Sammy78
    Sammy78 avatar
    27 posts
    Member since:
    May 2010

    Posted 23 Jul 2010 Link to this post

    Hi,

    I'm trying to change a ComboBoxItem's text from client side when then text of a radTextBox in another radPanel was changed. It seems to work (partially) but as soon as the page refreshes, I lose all changes and the radComboBox is repopulated with the default values, even though the new values are still present in the textbox. Here is the client side code for the change:
    Javascript:
    function RefreshPreneursPrenom(sender, args) {
                                var cell = sender.get_element().parentNode.parentNode;
                                var index = cell.parentNode.rowIndex - 1;
                                var prenom = (sender.get_element().value);
     
                                var combo = $find("<%= rcbPreneur.ClientID %>");
                                if (combo != null) {
                                    var items = combo.get_items();
                                    var comboItem = new Telerik.Web.UI.RadComboBoxItem();
                                    var comboItem = items.getItem(index);
     
                                    var nomComplet = comboItem.get_text();
                                    var nomComplet2 = nomComplet.split("  ");
                                    var nomFamille = nomComplet2[1];
                                    var nomComplet3 = (prenom.concat("  ", nomFamille));
     
                                    comboItem.set_text(nomComplet3);
                                    combo.trackChanges();
                                    combo.commitChanges();
                                    comboItem.Select;
     
                                    document.getElementById("<%= rapPreneur.ClientID %>").style.display = 'block';
                                }
                            }

    and here's the code that is executed when the panel is being populated:
    VB:
    Dim i As Int32
            Dim listeAssures As New OCollection(Of Assure)
     
            For i = 0 To NbreVoyageurs - 1
     
                Dim assure As New Assure
                Dim rgItem As GridDataItem = DirectCast(rgDetailsVoyageurs.MasterTableView.Items(i), GridDataItem)
                Dim item As New Telerik.Web.UI.RadComboBoxItem
     
                Dim id As New HiddenField
                id = DirectCast(rgItem("Prenom").FindControl("IDVoyageur" + (i + 1).ToString), HiddenField)
                assure.IDClient = CLng(id.Value)
     
                Dim prenom As New Telerik.Web.UI.RadTextBox
                prenom = DirectCast(rgItem("Prenom").FindControl("PrenomVoyageur" + (i + 1).ToString), Telerik.Web.UI.RadTextBox)
                assure.Prenom = prenom.Text
     
                Dim nom As New Telerik.Web.UI.RadTextBox
                nom = DirectCast(rgItem("Nom").FindControl("NomVoyageur" + (i + 1).ToString), Telerik.Web.UI.RadTextBox)
                assure.Nom = nom.Text
     
                'construction de la date de naissance
                Dim txtJour As New Telerik.Web.UI.RadNumericTextBox
                txtJour = DirectCast(rgItem("Date de naissance").FindControl("DateNaissanceJourVoy" + (i + 1).ToString), Telerik.Web.UI.RadNumericTextBox)
                Dim jourNaissance As Integer = CInt(txtJour.Value)
     
                Dim txtMois As New Telerik.Web.UI.RadComboBox
                txtMois = DirectCast(rgItem("Date de naissance").FindControl("DateNaissanceMoisVoy" + (i + 1).ToString), Telerik.Web.UI.RadComboBox)
                Dim moisNaissance As Integer = CInt(txtMois.SelectedValue.ToString)
     
                Dim txtAnnee As New Telerik.Web.UI.RadNumericTextBox
                txtAnnee = DirectCast(rgItem("Date de naissance").FindControl("DateNaissanceAnVoy" + (i + 1).ToString), Telerik.Web.UI.RadNumericTextBox)
                Dim anneeNaissance As Integer = CInt(txtAnnee.Value)
     
                assure.DateNaissance = New Date(anneeNaissance, moisNaissance, jourNaissance)
     
                Dim checkACharge As New CheckBox
                checkACharge = DirectCast(rgItem("A Charge").FindControl("AChargeVoy" + (i + 1).ToString()), CheckBox)
                assure.IsPersonneACharge = checkACharge.Checked
     
                Dim age As Integer = Today.Year - assure.DateNaissance.Year
     
                If assure.DateNaissance.Month > Today.Month OrElse _
                   (assure.DateNaissance.Month = Today.Month AndAlso assure.DateNaissance.Day > Today.Day AndAlso _
                    assure.DateNaissance.Day <> Today.Day) Then
                    'The current year is not yet complete.
                    age -= 1
                End If
     
                If (age > 15) Then
                    item.Text = assure.Prenom + "  " + assure.Nom
                    item.Value = assure.IDClient.ToString
                    rcbPreneur.Items.Add(item)
                End If
     
            Next
     
            Components.AddRange(listeAssures)
     
            rcbPreneur.DataBind()
     
            rapPreneur.Visible = True


    Any help would be greatly appreciated
    Thank you

    Sam
  2. Cori
    Cori avatar
    562 posts
    Member since:
    Jul 2010

    Posted 23 Jul 2010 Link to this post

    You should change your if statement to this:

    if (combo != null) {
                                    var items = combo.get_items();
                                    var comboItem = new Telerik.Web.UI.RadComboBoxItem();
                                    var comboItem = items.getItem(index);
       
                                    var nomComplet = comboItem.get_text();
                                    var nomComplet2 = nomComplet.split("  ");
                                    var nomFamille = nomComplet2[1];
                                    var nomComplet3 = (prenom.concat("  ", nomFamille));
       
                                    combo.trackChanges();
                                    comboItem.set_text(nomComplet3);
                                    comboItem.Select;
                                    combo.commitChanges();
       
                                    document.getElementById("<%= rapPreneur.ClientID %>").style.display = 'block';
                                }

    You called trackChanges() before you made the change and commitChanges() before you selected the item. You need to call trackChanges() before you make any changes and the call commitChanges() after you've made all your changes.

    I hope that helps.
  3. Sammy78
    Sammy78 avatar
    27 posts
    Member since:
    May 2010

    Posted 26 Jul 2010 Link to this post

    Hi Cori,
    Thanks for pointing out my mistake. Honestly, I had never used these commands (trackchanges and commit), so I just put them in there and tried some things out. But now, with your help, I understand now. :)
    I still have a little problem though: If the  value that I'M changing is the one selected in the combobox, the selected change won't change (until postback anyway). The value is alright in the dropdown part though.

    Any idea on how to fix this?
    Thanks
    Sam
  4. Sammy78
    Sammy78 avatar
    27 posts
    Member since:
    May 2010

    Posted 27 Jul 2010 Link to this post

    Anyone have any idea?

    Thanks in advance
    Sam
  5. Sammy78
    Sammy78 avatar
    27 posts
    Member since:
    May 2010

    Posted 27 Jul 2010 Link to this post

    I've found the solution to my problem!

    Javascript:
    function RefreshPreneursPrenom(sender, args) {
    var cell = sender.get_element().parentNode.parentNode;
    var indexGrille = cell.parentNode.rowIndex;
    var prenom = (sender.get_element().value);
     
    var combo = $find("<%= rcbPreneur.ClientID %>");
    if (combo != null) {
    var items = combo.get_items();
    var comboItem = new Telerik.Web.UI.RadComboBoxItem();
    var comboItem = items.getItem(indexGrille);
    var comboItemSelectionne = combo.get_selectedItem();
     
    var nomComplet = comboItem.get_text();
    var nomComplet2 = nomComplet.split("  ");
    var nomFamille = nomComplet2[1];
    var nomComplet3 = (prenom.concat("  ", nomFamille));
     
    combo.trackChanges();
    comboItem.set_text(nomComplet3);
    combo.updated;
    combo.commitChanges();
    combo.findItemByValue("zero").select();
    comboItemSelectionne.select();
     
    document.getElementById("<%= rapPreneur.ClientID %>").style.display = 'block';
    }
    }

    Thanks for the help and for putting me on the right path
    Sam
Back to Top