or
Hi All,
I have page with DropDownList and Telerik's Kendo UI Grid Control. When first time page is opened DropDownList has no item selected in it. When user selects value in DropDownList only then Grid should make Ajax call to the server and load corresponding data.
My code works fine when user selects item in DropDownList, but the problem is that first time when page is opened I there is no value in DropDownList and Grid should not make a call to the server.
My question is how can I prevent grid not to make a call to the server if there is no item selected in DropDowList?
Thanks a lot.
<div>@Html.Kendo().DropDownList().Name("broker").DataTextField("GrandParentName").DataValueField("Id").BindTo(Model).SelectedIndex(@selectedIndex).Events(e => e.Change("brokerChanged")) </div> @(Html.Kendo().Grid<OrderViewModel>() .Name("Orders") .HtmlAttributes(new {style = "height: 500"}) .Columns(c => { c.Bound(p => p.Id) .Width(50) .Title("") .Sortable(false) .IncludeInMenu(false) .ClientTemplate((@Html.ActionLink("Edit", "Index", "Splits", new {Id = "OrderId"}, null).ToHtmlString().Replace("OrderId", "#=Id#"))); c.Bound(p => p.TradeDate) .Title("Trd Dt") .Format("{0:d}") .Width(90) .HtmlAttributes(new {style = "text-align: right"}); c.Bound(p => p.Price) .Title("Price") .Format("{0:n}") .Width(100) .HtmlAttributes(new {style = "text-align: right"}); c.Bound(p => p.Status) .Title("Status"); c.Bound(p => p.Notional) .Title("Notional") .Format("{0:n}") .HtmlAttributes(new {style = "text-align: right"}); }) .Sortable() .Scrollable() .ColumnMenu() .Pageable(x => { x.Enabled(true); x.PreviousNext(false); x.PageSizes(false); x.Info(true); x.Input(false); x.Numeric(false); x.Refresh(true); x.Messages(y => y.Display("{2} Order(s)")); }) .Resizable(resize => resize.Columns(true)) .Reorderable(reoder => reoder.Columns(true)) .DataSource(ds => ds.Ajax() .ServerOperation(false) .Read(read => read.Action("Action", "MyController").Data("selectedBrokerId"))) )<script type="text/javascript"> function brokerChanged() { var grid = $("#Orders").data("kendoGrid"); grid.dataSource.read(); } function selectedBrokerId() { var obj = { brokerId: $("#broker").data("kendoDropDownList").value() }; return obj; }</script>@(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ProductViewModel>() .Name("Grid") .Columns(columns => {columns.Bound(p => p.ProductID).Groupable(false).Width(140);columns.Bound(p => p.ProductName);columns.Bound(p => p.UnitPrice).HtmlAttributes(new { style = "text-align: right" }).Width(140);columns.Bound(p => p.UnitsInStock).Width(160);}).Pageable() .Sortable() .Scrollable().Selectable(row => row.Mode(GridSelectionMode.Single).Type(GridSelectionType.Cell)) .Groupable() .DataSource(dataSource => dataSource.Ajax().Read(read => read.Action("Products_Read", "Grid"))))<button id="refreshButton" onclick="refreshGrid()">Start the Refresh</button><script> var myGrid; $(function () { myGrid = $('#Grid').data('kendoGrid'); }); function refreshGrid() { $('#refreshButton').attr("disabled", "disabled"); setInterval(DoWork, 1000); } function DoWork() { myGrid.dataSource.read(); }</script>using System.Collections.Generic;using System.Data.Linq;using System.Linq;using System.Web.Mvc;using Kendo.Mvc.Examples.Models;using Kendo.Mvc.Extensions;using Kendo.Mvc.UI;using System;namespace Kendo.Mvc.Examples.Controllers{ public partial class GridController : Controller { public ActionResult Index() { return View(GetProducts()); } public ActionResult Remote_Data() { return View("AjaxBinding"); } public ActionResult Products_Read([DataSourceRequest] DataSourceRequest request) { return Json(GetProducts().ToDataSourceResult(request), JsonRequestBehavior.AllowGet); } private static IEnumerable<OrderViewModel> GetOrders() { var northwind = new NorthwindDataContext(); var loadOptions = new DataLoadOptions(); loadOptions.LoadWith<Order>(o => o.Customer); northwind.LoadOptions = loadOptions; return northwind.Orders.Select(order => new OrderViewModel { ContactName = order.Customer.ContactName, OrderDate = order.OrderDate, OrderID = order.OrderID, ShipAddress = order.ShipAddress, ShipCountry = order.ShipCountry, ShipName = order.ShipName, EmployeeID = order.EmployeeID }); } private static IEnumerable<ProductViewModel> GetProducts() { //var northwind = new NorthwindDataContext(); var list = new List<ProductViewModel>(); var randomGen = new Random(DateTime.Now.Second); for (var i = 500; i >= 1; i--) { list.Add( new ProductViewModel { ProductID = i, ProductName = "ProductName-" + i, UnitPrice = Convert.ToDecimal(randomGen.NextDouble()), UnitsInStock = randomGen.Next(), UnitsOnOrder = (short)randomGen.Next(), Discontinued = (i % 2) == 0 , LastSupply = DateTime.Today.AddDays(i) } ); } return list.AsEnumerable(); //return northwind.Products.Select(product => new ProductViewModel //{ // ProductID = product.ProductID, // ProductName = product.ProductName, // UnitPrice = product.UnitPrice ?? 0, // UnitsInStock = product.UnitsInStock ?? 0, // UnitsOnOrder = product.UnitsOnOrder ?? 0, // Discontinued = product.Discontinued, // LastSupply = DateTime.Today //}); } private static IEnumerable<EmployeeViewModel> GetEmployees() { var northwind = new NorthwindDataContext(); return northwind.Employees.Select(employee => new EmployeeViewModel { EmployeeID = employee.EmployeeID, FirstName = employee.FirstName, LastName = employee.LastName, Country = employee.Country, City = employee.City, Notes = employee.Notes, Title = employee.Title, Address = employee.Address, HomePhone = employee.HomePhone }); } }}