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

[Solved] Prevent a default selection

12 Answers 761 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Nick
Top achievements
Rank 1
Nick asked on 11 Feb 2008, 08:08 PM

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

Sort by
0
Steele
Top achievements
Rank 1
answered on 11 Feb 2008, 11:42 PM
I need to do this too...

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.
0
Nick
Top achievements
Rank 1
answered on 12 Feb 2008, 12:51 AM
Yah so far the options I've found are to allow custom text, setting the text property to an empty string (which still leaves the first option 'selected'), and clearing it in client code on page load.

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.
0
Nikolay
Telerik team
answered on 13 Feb 2008, 01:26 PM
Hi there,

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
0
Steele
Top achievements
Rank 1
answered on 13 Feb 2008, 09:18 PM

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.

0
Nick
Top achievements
Rank 1
answered on 13 Feb 2008, 09:36 PM
Your solution works fine IF you want to allow custom text, however allowing custom text isn't an option for us so this won't work. If you don't allow custom text, the server side ClearSelection() doesn't do anything, however the client side clearSelection() does which is strange behavior.

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.
0
Nick
Top achievements
Rank 1
answered on 13 Feb 2008, 09:46 PM
A good analogy for my problem would be asking the user to select their state from the dropdown; I don't want to accept 'Alabama' as a default, but I also don't want to let the user fill in any arbitrary string either. :)
0
Nikolay
Telerik team
answered on 18 Feb 2008, 07:56 AM
Hi there,

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
0
macmariman
Top achievements
Rank 2
answered on 09 Jun 2008, 04:41 AM
Hello all,

Has this functionality been implemented?

Thanks,

Martín
0
Veselin Vasilev
Telerik team
answered on 11 Jun 2008, 12:06 PM
Hi guys,

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
0
Veselin Vasilev
Telerik team
answered on 27 Jun 2008, 07:02 AM
Hello Martin,

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
0
J
Top achievements
Rank 1
answered on 24 Jun 2009, 02:22 AM
I too had this problem and my solution was to do the following:

- 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.


Tags
ComboBox
Asked by
Nick
Top achievements
Rank 1
Answers by
Steele
Top achievements
Rank 1
Nick
Top achievements
Rank 1
Nikolay
Telerik team
macmariman
Top achievements
Rank 2
Veselin Vasilev
Telerik team
J
Top achievements
Rank 1
Share this question
or