Search

11 Ogos 2013

DropDownList

The DropDownList HtmlHelper extension is a server-side wrapper for the Kendo UI DropDownList widget.
Getting Started

There are two ways to bind a Kendo DropDownList for ASP.NET MVC:
server - the data will be serialized to the client. No Ajax requests will be made.
ajax - the dropdownlist will make ajax request to get the data.
Configure the Kendo DropDownList for server binding

Here is how to configure the Kendo DropDownList for server binding to the Northwind Products table using Linq to SQL:


Make sure you have followed all the steps from the Introduction help topic.


1.     Create a new action method and pass the Products table as the model:
  1. public ActionResult Index()
    {
        NorthwindDataContext northwind = new NorthwindDataContext();
    
    
    return View(northwind.Products);
    
    
    }
  2. Make your view strongly typed:
    • WebForms
      <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
         Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication1.Models.Product>>" %>
    • Razor
      @model IEnumerable<MvcApplication1.Models.Product>


  3. Add a server bound dropdownlist:


    WebForms
    • <%: Html.Kendo().DropDownList()
          .Name("productDropDownList") //The name of the dropdownlist is mandatory. It specifies the "id" attribute of the widget.
          .DataTextField("ProductName") //Specifies which property of the Product to be used by the dropdownlist as a text.
          .DataValueField("ProductID") //Specifies which property of the Product to be used by the dropdownlist as a value.
          .BindTo(Model)   //Pass the list of Products to the dropdownlist.
          .SelectedIndex(10) //Select an item with index 10\. Note that the indexes are zero based.
      %>
    • Razor
      @(Html.Kendo().DropDownList()
        .Name("productDropDownList") //The name of the dropdownlist is mandatory. It specifies the "id" attribute of the widget.
        .DataTextField("ProductName") //Specifies which property of the Product to be used by the dropdownlist as a text.
        .DataValueField("ProductID") //Specifies which property of the Product to be used by the dropdownlist as a value.
        .BindTo(Model)   //Pass the list of Products to the dropdownlist.
        .SelectedIndex(10) //Select an item with index 10\. Note that the indexes are zero based.
      )

Configure the Kendo DropDownList for ajax binding

Here is how to configure the Kendo DropDownList for ajax binding to the Northwind Products table using Linq to SQL:


Make sure you have followed all the steps from the Introduction help topic.


1.      Create an action method which renders the view:
  1. public ActionResult Index()
    {
        return View();
    }
  2. Create a new action method and pass the Products table as Json result:
    public JsonResult GetProducts()
    {
        NorthwindDataContext northwind = new NorthwindDataContext();
    
    
    return Json(northwind.Products, JsonRequestBehavior.AllowGet);
    
    
    }
  3. Add a ajax bound dropdownlist:


    WebForms
    • <%: Html.Kendo().DropDownList()
          .Name("productDropDownList") //The name of the dropdownlist is mandatory. It specifies the "id" attribute of the widget.
          .DataTextField("ProductName") //Specifies which property of the Product to be used by the dropdownlist as a text.
          .DataValueField("ProductID") //Specifies which property of the Product to be used by the dropdownlist as a value.
          .DataSource(source =>
          {
                 source.Read(read =>
                 {
                      read.Action("GetProducts", "Home"); //Set the Action and Controller name
                 })
                 .ServerFiltering(true); //If true the DataSource will not filter the data on the client.
          })
          .SelectedIndex(0) //Select first item.
      %>
    • Razor
      @(Html.Kendo().DropDownList()
          .Name("productDropDownList") //The name of the dropdownlist is mandatory. It specifies the "id" attribute of the widget.
          .DataTextField("ProductName") //Specifies which property of the Product to be used by the dropdownlist as a text.
          .DataValueField("ProductID") //Specifies which property of the Product to be used by the dropdownlist as a value.
          .DataSource(source =>
          {
                 source.Read(read =>
                 {
                      read.Action("GetProducts", "Home"); //Set the Action and Controller name
                 })
                 .ServerFiltering(true); //If true the DataSource will not filter the data on the client.
          })
          .SelectedIndex(0) //Select first item.
      )
Important:ToDataSourceResult() extension method will modify structure of the result and the widget will not be able to bind to it. Please return simple array of data in this case.

Accessing an Existing DropDownList

You can reference an existing DropDownList instance via jQuery.data(). Once a reference has been established, you can use the APIto control its behavior.


Accessing an existing DropDownList instance
//Put this after your Kendo DropDownList for ASP.NET MVC declaration
<script>
$(function() {
// Notice that the Name() of the dropdownlist is used to get its client-side instance
var dropdownlist = $("#productDropDownList").data("kendoDropDownList");
});
</script>

Handling Kendo UI DropDownList events

You can subscribe to all events exposed by Kendo UI DropDownList:

WebForms - subscribe by handler name

<%: Html.Kendo().DropDownList()
    .Name("dropdownlist")
    .BindTo(new string[] { "Item1", "Item2", "Item3" })
    .Events(e => e
        .Select("dropdownlist_select")
        .Change("dropdownlist_change")
    )
%>
<script>
function dropdownlist_select() {
    //Handle the select event
}

function dropdownlist_change() {
    //Handle the change event
}
</script>
Razor - subscribe by handler name
@(Html.Kendo().DropDownList()
  .Name("dropdownlist")
  .BindTo(new string[] { "Item1", "Item2", "Item3" })
  .Events(e => e
        .Select("dropdownlist_select")
        .Change("dropdownlist_change")
  )
)
<script>
function dropdownlist_select() {
    //Handle the select event
}

function dropdownlist_change() {
    //Handle the change event
}
</script>
Razor - subscribe by template delegate
@(Html.Kendo().DropDownList()
  .Name("dropdownlist")
  .BindTo(new string[] { "Item1", "Item2", "Item3" })
  .Events(e => e
      .Select(@<text>
        function() {
            //Handle the select event inline
        }
      </text>)
      .Change(@<text>
        function() {
            //Handle the change event inline
        }
        </text>)
  )
)
Page rendered at 8/11/2013 8:07:37 PM UTC.

Tiada ulasan:

Catat Ulasan

Buat BLOG Sendiri

  1. Permulaan : Daftar Akaun Blogspot
  2. Post Entri Dalam Blog
  3. Upload gambar dalam blog
  4. Upload video dalam blog
  5. Letak video dari Youtube dalam blog
  6. Letak kod HTML/javascript dalam blog
  7. Setting asas untuk blog
  8. Cara backup template blog
  9. Website untuk download template blog
  10. tukar template blog
  11. Buka link apabila klik gambar
  12. Background bagi setiap entry
  13. Letak iklan nuffnang statik
  14. Disable right click
  15. scroll-box-untuk-widget
  16. button-back-to-top
  17. Letak background dalam entri
  18. Letak-widget-popular-post-dalam-blog
  19. cara buat textbox dalam blog
  20. Dropdown menu dalam blog
  21. Image magnify
  22. Tutorial Link warna-warni (rainbow link)
  23. Buat gambar bergerak (scrolling image)
  24. Ketahui komen pada entri melalui email
  25. Letak icon pada URL blog
  26. Daftar dan letak shoutmix pada blog
  27. Read more dalam blog : Cara 1
  28. Read more dalam blog : Cara 2
  29. Letak Signature Dalam Blog
  30. Letak website lain dalam blog
  31. Daftar dan letak linkwithin
  32. Tukar ayat asal linkwithin
  33. Ketahui bilangan pengunjung online
  34. Ubah lebar ruangan komen
  35. Letak website lain dalam entri
  36. Tukar warna background blockquote
  37. Background bergambar pada blockquote
  38. Letak gambar sebelah tajuk entri
  39. Letak tajuk entri pada readmore
  40. Perkataan bergerak (scrolling/marquee text)
  41. Letak salji dalam blog
  42. Ruangan komen bawah entri
  43. Video player kepada mp3 player
  44. Cara dapatkan url gambar
  45. Sistem rating pada blog
  46. Recent Comment dalam blog
  47. Widget Popular Post
  48. Tutorial Widget Popular Post
  49. Tukar Cursor Dalam Blog
  50. Jadikan Cursor Berkilau
  51. Animated Recent Post Widget
  52. Recent Post with Thumbnail
  53. Misterlinky - Cara Mudah Pengunjung Blog Tinggalkan Link
  54. Saiz Gambar Maksimum Dalam Blog
  55. Ubah Lebar Gambar Dalam Entri
  56. Jumlah Post dan Komen dalam Blog
  57. Facebook Share Button
  58. Facebook Like Button
  59. Button Like Untuk Blog
  60. Scrollbox Untuk Widget
  61. Disable highlight dalam blog
  62. Ketahui kelajuan load element blog
  63. Cara letak sitemap dalam blog
  64. Bubble cursor untuk blog
  65. Tutorial gerakkan title blog
  66. Tutorial hasilkan title blog bergerak
  67. Tips menaip entri dalam blog part 1
  68. Tips menaip entri dalam blog part 2
  69. Tukar warna text highlight
  70. Following star pada cursor
  71. Tutorial perkataan ikut cursor
  72. Letak emotion icon dalam komen
  73. Cara unfollow blog
  74. Tukar gambar apabila dilalukan cursor
  75. Kesan khas pada gambar apabila dilalukan cursor
  76. Cara buat scroll box untuk blog archive
  77. Tutorial tab menu / navbar menu
  78. Energy saver untuk blog anda
  79. Animated recent post ver2