RadListBox - at least have one item

4 posts, 0 answers
  1. Mar
    Mar avatar
    25 posts
    Member since:
    Apr 2007

    Posted 13 Oct 2009 Link to this post

    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
  2. Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 15 Oct 2009 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Todd
    Todd avatar
    3 posts
    Member since:
    Sep 2008

    Posted 25 Oct 2010 Link to this post

    How do I do this when my RadListBox is within a FormView's EditItemTemplate?
  5. Genady Sergeev
    Admin
    Genady Sergeev avatar
    1596 posts

    Posted 28 Oct 2010 Link to this post

    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
Back to Top