Select default item from the dropdownlist if Item count is 1

2 posts, 0 answers
  1. Amol
    Amol avatar
    4 posts
    Member since:
    Apr 2013

    Posted 29 Jul 2013 Link to this post

    Hi,

    I have a cascading dropdownlist,  and if the Item count for the dropdown getting populated is 1 I want to keep the item it default selected instead of showing OptionLabel and the item. Or else remove the Option label in that case. below of the my dropdown which gets populated on change of dropdown 'DeliveryDestinationID'. How do i do this.

    @(Html.Kendo().DropDownList()
                                          .Name("DeliveryNameID")
                                          .HtmlAttributes(new { style = "width:235px" })
                                          .OptionLabel("Select Delivery Name...")
                                          
                                          .DataTextField("Text")
                                          .DataValueField("Value")
                                         .DataSource(source =>
                                          {
                                              source.Read(read =>
                                              {
                                                  read.Action("GetDeliveryNames", "DeliveryQueue")
                                                      .Data("filterAsPerServiceProvider");
                                              })
                                              .ServerFiltering(true);
                                          })
                                          .Enable(false)
                                          .AutoBind(false)
                                          .CascadeFrom("DeliveryDestinationID")
                                        )



  2. Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2156 posts

    Posted 31 Jul 2013 Link to this post

    Hello Amol,


    This is a quote from my answer in the support thread:

    To achieve this you could bind to the dataBound event of the DropDownList and check the length of the dataSource data. If there is only one item, you could select it.
    E.g.
    .Events(e => e.DataBound("dataBound"))

    function dataBound(e) {
        var ds = this.dataSource.data();
        if (ds.length == 1) {
            this.select(1);
        }
    }



    Please post your questions only once, so we could provide you the best possible assistance. I wish you a great day!

     

    Regards,
    Dimiter Madjarov
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. Kendo UI is VS 2017 Ready
Back to Top