Andrea Azzola

Tips and Techniques for Lifestyle Design

Simulating a RequiredFieldValidator when using a RadComboBox

Posted on [Permalink]

Telerik's RadComboBox does not behave like a traditional DropDownList. You may consider to use a CustomValidator instead, here is the code:

Step 1: Markup

<asp:Label ID="lblExample" runat="server" AssociatedControlID="rcbExample" 
	Text="Example" />
<telerik:RadComboBox ID="rcbExample" runat="server" />
<asp:CustomValidator ID="cvlExample" runat="server" ControlToValidate="rcbExample" 
	Text="*" ClientValidationFunction="cvlExampleValidate" 
	OnServerValidate="cvlExample_ServerValidate" />

Step 2: Client-side validation:

function cvlExampleValidate(source, args) {
	args.IsValid = radComboValidate("<%= rcbExample.ClientID %>");
}

function radComboValidate(controlName) {
	var combo = $find(controlName);
	var text = combo.get_text();

	if (text.length < 1)
		return false;
	else {
		var node = combo.findItemByText(text);
		if (node) {
			var value = node.get_value();

			if (value.length > 0)
				return true;
		}
		else
			return false;
	}
}

Step 3: Server-side valitation:

protected void cvlStatus_ServerValidate(object source, ServerValidateEventArgs args)
{
	args.IsValid = rcbStatus.SelectedValue.Length > 0;
}
Categories:

Comments

Thanks, this was useful.

~Tom