ASP.Net AJAX Client Side Template

by ckal 22. September 2009 18:30
  • Add javascript frameworks from CDN.

    <script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js" type="text/javascript"></script>   
    <script src="http://ajax.microsoft.com/ajax/beta/0909/MicrosoftAjax.js" type="text/javascript"></script>
    <script src="http://ajax.microsoft.com/ajax/beta/0909/MicrosoftAjaxTemplates.js" type="text/javascript"></script>
  • Create Client Side Template

    <h4>Propular Items</h4>
    <table id="popularItems" class="popularItems sys-template">
       <tr>
          <td>{{ ProductName }}</td>
          <td>{{ NumberOrdered }}</td>                    
       </tr>
    </table>
  • Server Side JSON

    public JsonResult BestSellers(int? id)
    {
       var bestSellers = from p in _salesRepository.GetProductTotals(DateTime.Parse("7/7/2009"), DateTime.Now)
                         select new {p.ProductName, p.NumberOrdered};
    
       if (id != null)
          bestSellers = bestSellers.Take(id.Value);
    
       return Json(bestSellers);
    }
  • Client JS – Pulling it all together

    <script language="javascript" type="text/javascript">
            function pageLoad() {
    
                var url = 'http://localhost/Dashboard/Dashboard/';
                var top = 10;
    
                var productDate = [];
                $(function() {
                $.getJSON( url + 'BestSellers/' + top, null, function(json) {
                         Sys.Observer.addRange(productDate, json);
                    });
                });            
                
                $create(Sys.UI.DataView, {
                    data: productDate
                }, null, null, $get("popularItems"));
    
            }
        </script>

Tags:

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About the author

Something about the author

Tag cloud

    Month List

    Page List