OnClientItemChecked and set_text issue

6 posts, 0 answers
  1. Ivan
    Ivan avatar
    3 posts
    Member since:
    Nov 2013

    Posted 27 Nov 2013 Link to this post

    Hello everyone, I have one problem with RadComboBox and will appreciate any help. I'm using 2013.1.403.40 version with v4.0.30319 runtime in my ASP.NET application. So, here is my solution: combobox on the page, that is populated from server side.
    <telerik:radcombobox id="cmbBox" runat="server" width="185px"
    DataTextField="Text" DataValueField="Value" DataCheckedField="IsChecked" MaxHeight="185px"
    CheckBoxes="True" EnableCheckAllItemsCheckBox="True" Filter="None" Sort="None" EnableTextSelection="False" OnClientItemChecked="cmbBoxItemChecked"/>
    cmbBox.Items.Add(new RadComboBoxItem("separator1") { IsSeparator = true });
    cmbBox.Items.Add(new RadComboBoxItem
                        {  Text = "Name1",   Value = "Value1",  Checked = false });
    cmbBox.Items.Add(new RadComboBoxItem("separator2") { IsSeparator = true });
    cmbBox.Items.Add(new RadComboBoxItem
                        {  Text = "Name2",   Value = "Value2",  Checked = false });
    cmbBox.Items.Add(new RadComboBoxItem("separator3") { IsSeparator = true });
    cmbBox.Items.Add(new RadComboBoxItem
                        {  Text = "Name3",   Value = "Value3",  Checked = false });
    So, 6 items will appear on client side (3 separators and 3 simple items). I've also attached event handler for OnClientItemChecked with folowing logic:

    function cmbBoxItemChecked(sender, eventArgs)
    {
       sender.set_text("my custom text according to internal logic, based on checked item");
    }
    And here is my problem: for example, before sender.set_text operation, text was "2 items checked". Right after sender.set_text it will change to "my custom..." (as desired).
    But after exiting from event handler, text will return to it's previous state ("2 items checked"). It seems, that another event fires right after 
    OnClientItemChecked.
    So my desired behaviour, is to change text in radcombobox after user was (un)checked random item. It is possible, or by now, this is not achievable ? 
    Thank you in advance.

  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 27 Nov 2013 Link to this post

    Hi Ivan,

    Please have a look into the following JavaScript to set a text on OnClientItemChecked event.

    JavaScript:
    <script type="text/javascript">
        function cmbBoxItemChecked(sender, eventArgs) {
            if (eventArgs.get_item()._checkBoxElement.checked == true) {
                sender.set_text("my custom text according to internal logic, based on checked item");
                sender.ClearSelection();
            }
        }
    </script>

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Ivan
    Ivan avatar
    3 posts
    Member since:
    Nov 2013

    Posted 28 Nov 2013 Link to this post

    Hi Shinu, many thanks for you quick answer. Unfortunately, this did not work. This clearselection method do not prevent text changes AFTER event has ended. For example, 
    //initial text "2 items checked"
    alert(sender.get_text()); // "2 items checked"
    sender.set_text("test");
    alert(sender.get_text()); // "test"
    sender.clearSelection();
    alert(sender.get_text()); "empty message" and all selection is cleared, which i did not prefer to do, BTW
    }
    // event handler ended and text again returned to "2 items checked"
    Maybe there are another ways to do this ? Maybe with native js and DOM ?
    Thank you.
  5. Nencho
    Admin
    Nencho avatar
    1457 posts

    Posted 02 Dec 2013 Link to this post

    Hello Ivan,

    Please consider setting an appropriate time out for the execution of the desired text editing in the following manner:
    function cmbBoxItemChecked(sender) {
               setTimeout(function (e) {
                   sender.set_text("my custom text according to internal logic, based on checked item");
               }, 10)
           }


    Regards,
    Nencho
    Telerik
    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 the blog feed now.
  6. Ivan
    Ivan avatar
    3 posts
    Member since:
    Nov 2013

    Posted 02 Dec 2013 Link to this post

    Thank you for advice. Actually, I've also applied this solution, because there is no another good alternative. However, I think this is workaround, because timeout may behave differently on all browsers, and if user will quickly recheck items, there will be visible UI changes and freezes. Maybe, Telerik team should consider to implement event for this purpose, for example, after text actually was changed in header. Existing OnClientTextChange do not fit for this purpose. And i think, there is a bug in implementation. If we have both OnClientItemChecking and OnClientItemChecked it's seems logical, that on "ed" event all actions were already performed by control, but it is not.
    Anyway, thanks again, and I will hope this will help anyone who will face the same problem.

  7. Nencho
    Admin
    Nencho avatar
    1457 posts

    Posted 05 Dec 2013 Link to this post

    Hello Ivan,

    Regarding your implementation of the RadComboBox and the fact that the CheckBoxes feature is enabled the control is in read-only state. In such scenario altering the text of the input of the control is  consider as hack, since it should correspond to the checked items text. This is the reason for the provided workaround.

    Regards,
    Nencho
    Telerik
    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 the blog feed now.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017