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

RadListBox - at least have one item

3 Answers 153 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Mar
Top achievements
Rank 1
Mar asked on 13 Oct 2009, 03:01 PM
Hello,

I am building a page similar to the following:
http://demos.telerik.com/aspnet-ajax/listbox/examples/applicationscenarios/insertitem/defaultcs.aspx

One of the requirements is that as part of the validation, I have to check that at least one item is present inside the RadListBox.  I tried using the RequiredFieldValidator but that enforces the validation that the item in the listbox should be selected.

In my case, I just want to check that the listbox has at least one item.  (I don't want my users to select one item for this validation to succeed).  How can I accomplish this that the validation succeeds if the listbox has one item which may or may not be selected?

Thanks,
Pratik

3 Answers, 1 is accepted

Sort by
0
Veselin Vasilev
Telerik team
answered on 15 Oct 2009, 01:15 PM
Hi Pratik,

In your case you need to use a CustomValidator. In its client validation function you can find the listbox and check the number of its items. Here is an example:

<script type="text/javascript">
function validateListbox(source, args)
{
   var list = $find("<%= RadListBox1.ClientID %>");
   args.IsValid = list.get_items().get_count() > 0;
}
</script>


Best wishes,
Veselin Vasilev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Todd
Top achievements
Rank 2
answered on 25 Oct 2010, 08:02 PM
How do I do this when my RadListBox is within a FormView's EditItemTemplate?
0
Genady Sergeev
Telerik team
answered on 28 Oct 2010, 04:54 PM
Hello Todd,

You can try the following, in the ItemCommand event of FormView use the following code:

protected void FormView1_ItemCommand(object sender, FormViewCommandEventArgs e)
{
     
    if (e.CommandName == "Update")
    {
        var listBox = (sender as FormView).FindControl("RadListBox") as RadListBox;
        ClientScript.RegisterClientScriptBlock(typeof(Page), "listbox", string.Format("window['listbox']={0}", listBox.ClientID));
    }
}

Then use window['listbox'] to obtain the id of the listbox:

function validateListbox(source, args)
{
   var list = $find("windod['listbox']");
   args.IsValid = list.get_items().get_count() > 0;
}


Regards,
Genady Sergeev
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
Tags
ListBox
Asked by
Mar
Top achievements
Rank 1
Answers by
Veselin Vasilev
Telerik team
Todd
Top achievements
Rank 2
Genady Sergeev
Telerik team
Share this question
or