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

set_SelectedIndex does nothing!

11 Answers 155 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 20 Oct 2011, 04:19 PM
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>

11 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 21 Oct 2011, 06:29 AM
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.
0
Robert
Top achievements
Rank 1
answered on 21 Oct 2011, 02:40 PM
Thanks Princy... you always come through.

-R
0
Graeme Mc
Top achievements
Rank 1
answered on 28 Apr 2015, 08:58 AM

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

0
Graeme Mc
Top achievements
Rank 1
answered on 28 Apr 2015, 09:23 AM

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?

0
Nencho
Telerik team
answered on 01 May 2015, 07:49 AM
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.

 
0
Graeme Mc
Top achievements
Rank 1
answered on 05 May 2015, 09:48 AM

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?

0
Nencho
Telerik team
answered on 08 May 2015, 06:56 AM
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.

 
0
Graeme Mc
Top achievements
Rank 1
answered on 13 May 2015, 09:50 AM

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

0
Nencho
Telerik team
answered on 18 May 2015, 07:15 AM
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
0
Graeme Mc
Top achievements
Rank 1
answered on 18 May 2015, 08:53 AM

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.

0
Nencho
Telerik team
answered on 21 May 2015, 07:30 AM
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
Tags
ComboBox
Asked by
Robert
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Robert
Top achievements
Rank 1
Graeme Mc
Top achievements
Rank 1
Nencho
Telerik team
Share this question
or