What is the best way to ensure that there is no default selection in my combo box? I can clear it client side with:
| var combo = $find("<%= RadComboBox1.ClientID %>"); |
| combo.clearSelection(); |
but I'd prefer to be able to clear it server side or even better just tell it not to select anything by default in the first place. I've tried the server side .ClearSelection() but I can't seem to get it to work. No matter where I call it (onload, after databind, etc) the first item in the list is still selected.
12 Answers, 1 is accepted
It seems dumb today to have to insert an empty data element to allow a combobox to be cleared, I am sure there is a better way built in.
but I can't find it either.
I'm working on an implementation of multi select with the option for 'select all/none' (which is working pretty well) but because that's the first option and selected by default you can't 'check' it because the OnClientSelectedIndexChanging event won't fire till you select another element.
I prepared a small project for you. Please download the files and give them a go. I used AllowCustomText="true" and and I set the Text to an empty string before clearing the items. Everything ran as expected after calling the ClearSelection() server method. What am I missing?
Sincerely yours,
Nick
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
AllowCustomText sort of works, though it is not exactly what I was talking about.
I am fine with AllowCustomText being the mechanism we use here and for the most part it does work fine.
However. lets say I have a list of states. If a user enters ZZ into the combobox, it just doesn't select anything, which is fine, but the user is still expecting ZZ to be expected as a valid input and in my case the results are not what is expected.
User enters in ZZ to the States combobox and runs a report. What he gets back is instead, a report with no state defined (which is allowed), when what he should get back is "State ZZ does not exist... etc." I really don't want to do all that error handling and thus the entire reason I DO NOT want AllowCustomText in the first place.
I suppose one way is to disable the submit button until the SelectionChanged event fires, however, my DEFAULT is to accept NO SELECTION and that was all I was trying to achieve. The same effect would be to run this.clearSelection() in the onload event clientside, but that won't work on a DIV since it has no onload event. Placing:
img.Attributes("onload") = "javascript:var o=$find('" & ctl.ClientID & "');o.clearSelection();"
won't work for some reason because $find does not find the combobox (even though document.getElementById does)
I am thinking about using .OnClientKeyPressing() and disabling the submit button until .SelectionChanged fires, but this seems sort of kludgy to me.
Regardless, I think I am going to have to wsrite some custom errorchecking, or just accept that if a user enters CustomText that is not in the list, then it just ignores it and treats it as "No Selection".
While this will probably be OK, I will need to warn my users that if they enter keyboard selections, they may not get the expected results they are looking for.
I think in the end, what I would like to see from radComboBox is that when .AllowCustomText = False there is a property called, DefaultNoSelection and we can set that to True to achive "No Selection" when the combobox loads.
Idealy, if none of them items were marked as 'selected', the default selection would be null and you could specify that you'd like the first item selected by default, but i'd settle for an option to turn off the default selection functionality or at least clear it from the server side.
Thanks for all your explanations. They seem quite reasonable indeed. We will surely consider including a property that would enable or disable the default selection for the official Q1 edition of the control. If the property is available earlier than that, we will let you know.
Regards,
Nick
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
Has this functionality been implemented?
Thanks,
MartÃn
You can achieve the required behavior by setting the MarkFirstMatch property to True, leave AllowCustomText to False and set the Text property of the combobox:
| <telerik:RadComboBox ID="RadComboBox1" |
| MarkFirstMatch="True" |
| Text="Please select a state" |
| AllowCustomText="false" |
| runat="server"> |
| <Items> |
| <telerik:RadComboBoxItem runat="server" Text="RadComboBoxItem1" /> |
| <telerik:RadComboBoxItem runat="server" Text="RadComboBoxItem2" /> |
| <telerik:RadComboBoxItem runat="server" Text="RadComboBoxItem3" /> |
| </Items> |
| </telerik:RadComboBox> |
This way the user will be allowed to enter only a valid text in the combobox and when the page loads the Text will be shown.
I hope this helps.
Sincerely yours,
Veskoni
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
I suggest that you check this article which explains how to use required field validators with RadComboBox:
http://www.telerik.com/help/aspnet-ajax/combo_behaviorvalidation.html
Also, you might find this article helpful:
http://www.telerik.com/help/aspnet-ajax/validate-combobox-value.html
Greetings,
Veskoni
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
- Add a blank item to the items collection
- Set its visible property to false
- Set appenddatabounditems to true
Not sure how this works with validators, but in my case they weren't required and it worked a treat.
