set_SelectedIndex does nothing!

12 posts, 1 answers
  1. Robert
    Robert avatar
    198 posts
    Member since:
    Aug 2009

    Posted 20 Oct 2011 Link to this post

    I am trying to set the combo box to index 0 but nothing happens.   What is wrong?

    <head runat="server">
      <title></title>
    </head>
    <body>
      <form id="form1" runat="server">
      <div>
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        </telerik:RadScriptManager>
        <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
          <script type="text/javascript">
      
      
            function ResetScreen() {
      
              var oCbo = $find("<%=cboTransferMethod.ClientID %>");
              oCbo.set_selectedIndex(0);
            }
      
          </script>
        </telerik:RadCodeBlock>
        <telerik:RadComboBox ID="cboTransferMethod" runat="server" Skin="Simple" TabIndex="1"
          ToolTip="Transfer Method">
          <Items>
            <telerik:RadComboBoxItem runat="server" Text="Email" Value="E" />
            <telerik:RadComboBoxItem runat="server" Text="FTP" Value="F" />
            <telerik:RadComboBoxItem runat="server" Text="Move To Folder" Value="M" />
          </Items>
        </telerik:RadComboBox>
        <asp:Button ID="btnClear" runat="server" OnClientClick="ResetScreen(); return false;"
          TabIndex="1" Text="Clear" ToolTip="Clear" UseSubmitBehavior="false" />
      </div>
      </form>
    </body>
    </html>
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 21 Oct 2011 Link to this post

    Hello Robert,

    You can try the following javascript.

    JS:
    <script type="text/javascript">
    function ResetScreen()
    {
       var oCbo = $find("<%=cboTransferMethod.ClientID %>");
       oCbo.get_items().getItem(0).select();
    }
    </script>

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Robert
    Robert avatar
    198 posts
    Member since:
    Aug 2009

    Posted 21 Oct 2011 Link to this post

    Thanks Princy... you always come through.

    -R
  5. Graeme Mc
    Graeme Mc avatar
    17 posts
    Member since:
    Aug 2009

    Posted 28 Apr 2015 Link to this post

    Hi,

    How do I set selectedIndex to -1 in javascript?

    As mentioned above, combo.set_selectedIndex(-1); does nothing. But I can't use your solution because combo.get_items().getItem(-1).select(); fails!

     

    Thanks,

    Graeme

  6. Graeme Mc
    Graeme Mc avatar
    17 posts
    Member since:
    Aug 2009

    Posted 28 Apr 2015 in reply to Princy Link to this post

    I meant to add that combo.clearSelection() only half solves the problem. Indeed, it clears the selected item and the combobox goes back to the Empty Message, but the problem is that if I then select the item that was previously selected, no postback is fired, presumably because the control thinks the selectedIndex hasn't changed.

    Is there another way of telling the control that the selectedIndex was changed, but without causing a postback in the javascript function?

  7. Nencho
    Admin
    Nencho avatar
    1457 posts

    Posted 01 May 2015 Link to this post

    Hello Graeme,

    I tried to replicate the described issue, but it seems that the event is triggered correctly, even if the previously selected item is re-selected. Pleas find attached the sample example, which I have used for testing purpose. Are you able to replicate the problem with the sample? If not, specify how the implementation at your end differs from the provided one.

    In addition, specify which version of our controls are you using and under which browser the problem occurs.

    Regards,
    Nencho
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
  8. Graeme Mc
    Graeme Mc avatar
    17 posts
    Member since:
    Aug 2009

    Posted 05 May 2015 in reply to Nencho Link to this post

    Thanks for your reply Nencho.

    The initial post was about setting the combo to the item at index 0, in javascript. In your sample you're clearing the selection on server-side.

    If I use your solution above, ie: oCbo.get_items().getItem(0).select(); it causes a postback (which I don't want), so I tried to use oCbo.clearSelection(), which doesn't trigger a postback. It works fine in that it clears the combobox selection (sets it back to the empty message), and works fine for selecting all items in the list except for the item that was previously selected. It's as if the combo thinks it hasn't changed it's selected index, so OnSelectedIndexChanged() isn't fired.

    Does that make sense?

  9. Nencho
    Admin
    Nencho avatar
    1457 posts

    Posted 08 May 2015 Link to this post

    Hello Robert,

    If I had correctly understood you, using the last suggested approach (ClearSelection at server-side) - when you select an item, then use the button to clear the selection and then try to select the very same item no postback occurs? If this is the case, I am afraid that I am unable to replicate it locally, using the attached example. Are you able to replicate it with it?

    Please correct me if I had misunderstood the problematic behavior at your end.

    Regards,
    Nencho
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
  10. Graeme Mc
    Graeme Mc avatar
    17 posts
    Member since:
    Aug 2009

    Posted 13 May 2015 in reply to Nencho Link to this post

    Hi Nencho, I presume that last reply was for me, not Robert?:-)

    No, that's not the problem I'm talking about. It's pretty simple. If you have "Item 1" selected in a combobox, and then on some other event (like the click of a button), the dropdown gets cleared in javascript (using combo.clearSelection()). If you then select "Item 1" from the combobox, a postback is not fired!

    If any other item is selected, then it does get fired, so I'm presuming it's because the clearing was done in javascript, so when it  goes back to the server, it doesn't know that the selectedIndex was changed. My question is, is there a way to "tell" the control that the selectedIndex has now been changed to -1 when I cleared the selection. Or is there another way to solve this?

     Thanks

  11. Nencho
    Admin
    Nencho avatar
    1457 posts

    Posted 18 May 2015 Link to this post

    Hello Graeme,

    Please excuse me for mixing the names - indeed the last reply is for you. Generally, if the postback restores the index this should be reflected on the text displayed in the input of the combo. However, you can try to ensure that the changes are applied on the postback, you can use the approach that we suggest for those cases, which is using the trackChanges and commitChanges functions on the RadComboBox. Please consider the below:

    <script type="text/javascript">
                function clearComboSelection() {
                    var combo = $find("RadComboBox1");
                    combo.trackChanges();
                    combo.clearSelection();
                    combo.commitChanges();
                }
            </script>


    Regards,
    Nencho
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  12. Graeme Mc
    Graeme Mc avatar
    17 posts
    Member since:
    Aug 2009

    Posted 18 May 2015 in reply to Nencho Link to this post

    Thanks Nencho. I had tried this approach before, and I just tried it again. But the issue is still there - when selecting the previously selected item, the OnSelectedIndexChanged event is not fired. Yet, when selecting another item in the list, the event does get fired.

    I thought that possibly due to the complexity of our controls, something was getting lost along the way, but the fact that the combo works correctly for all other items means it's not that.

    We've had to use a workaround of adding a "--None--" item at index 0, and then in javascript:   combo.get_items().getItem(0).set_selected(true);

    I was hoping for a cleaner solution though.

  13. Nencho
    Admin
    Nencho avatar
    1457 posts

    Posted 21 May 2015 Link to this post

    Hello Graeme,

    Could you please post the declaration of the RadComboBox that you use at your end and specify the version of our controls that you are currently using. Thus we could perform some further tests and try to replicate the described problem.

    Regards,
    Nencho
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017