Client templating with jQuery and JSON (VBASPNETClientTemplateJQueryJSON)


This project illustrates how to display a tabular data to users based on some inputs in ASP.NET application. We will see how this can be addressed with JQuery and JSON to build a tabular data display in web page. Here we use JQuery plug-in JTemplate to make it easy.


Please follow these demonstration steps below.
Step 1: Open the VBASPNETClientTemplateJQueryJSON.sln.
Step 2: Expand the VBASPNETClientTemplateJQueryJSON web application and press Ctrl + F5 to show the Default.aspx.
Step 3: You can find an HTML table on the Default.aspx page, the tabular data display by JQuery plug-in JTemplate.

Code Logical

Step 1. Create a VB "ASP.NET Empty Web Application" in Visual Studio 2010 or Visual Web Developer 2010. Name it as "VBASPNETClientTemplateJQueryJSON".
Step 2. Add two JQuery library files in JS folder of the application, These JQuery library can help us create the JQuery function and JTemplate HTML table.
Step 3. Create an ASP.NET folder named "App_Code", and add Person entity class in it. The Person class is created as the data source of table.
Step 4. Add a web form and name it as "Default.aspx" in the root directory of application. The HTML table host in default page with JQuery functions.
Step 5  The JQuery functions can receive the JSON string from code-behind file and constructing an HTML table using JQuery by plug-in JTemplate. The HTML code as shown below:
    <html xmlns=""    <head runat="server"    <title></title> 
    <script src="JS/jquery-1.4.4.min.js" type="text/javascript"></script> 
    <script src="JS/jquery-jtemplates.js" type="text/javascript"></script> 
    <style type="text/css"> 
        .jTemplates {  
                        background: #FF00FF;  
                        border: 1px solid #000;  
                        margin: 2em;  
    <script type="text/javascript"> 
        function GetJSONString() { 
                url: "Default.aspx/PersonList", 
                type: "POST", 
                data: "{pageSize:5}", 
                dataType: "json", 
                contentType: "application/json; charset=utf-8", 
                function success(jsonString) { 
                function (json, status, e) { 
                    var err = JSON.parse(json.responseText); 
    <body onload="return GetJSONString()"    <form id="form1" runat="server"    <b>build a tabular data display in web page.</b> 
    <div    <script id="jTemplate" type="text/html">    
        <table class="jTemplates">    
               <colgroup width="100px"></colgroup> 
               <colgroup width="100px"></colgroup> 
               <colgroup width="125px"></colgroup> 
               <colgroup width="150px"></colgroup> 
               <colgroup width="150px"></colgroup> 
               <colgroup width="125px"></colgroup> 
               <colgroup width="*"></colgroup> 
                    <td style="border-style:dashed;;">Name</td>    
                    <td style="border-style:dashed;">Age</td>    
                    <td style="border-style:dashed;">Country</td>    
                    <td style="border-style:dashed;">Address</td>    
                    <td style="border-style:dashed;">Mail</td>    
                    <td style="border-style:dashed;">Telephone</td>   
                    <td style="border-style:dashed;">Comment</td>  
            {#foreach $T.d as Person}    
                    <td style="border-style:dashed;">{ $T.Person.Name }</td>    
                    <td style="border-style:dashed;">{ $T.Person.Age }</td>    
                    <td style="border-style:dashed;">{ $T.Person.Country }</td>    
                    <td style="border-style:dashed;">{ $T.Person.Address }</td>    
                    <td style="border-style:dashed;">{ $T.Person.Mail }</td>    
                    <td style="border-style:dashed;">{ $T.Person.Telephone }</td>  
                    <td style="border-style:dashed;">{ $T.Person.Comment }</td> 
    <br />  
    <div id="Zone" 
Step 6. The Person entity class file provide basic person properties, such as Name, Age, Country, e-mail, Address, Telephone and comments. This class need be a serializable class.
Visual Basic
    <Serializable()> _ 
    Public Class Person 
        Public Property Name As String 
        Public Property Age As Integer 
        Public Property Country As String 
        Public Property Address As String 
        Public Property Mail As String 
        Public Property Telephone As String 
        Public Property Comment As String 
    End Class
Step 7. Create a PersonList method for returning a list of person entity instances as the JSON string that it will render on the default page.
Visual Basic
    ''' <summary> 
    ''' This method is used to provide JSON string variable. 
    ''' </summary> 
    ''' <param name="pageSize"></param> 
    ''' <returns></returns> 
    <WebMethod()> _ 
    Public Shared Function PersonList(ByVal pageSize As IntegerAs List(Of Person) 
        Dim _personList As New List(Of Person)() 
        Dim person As New Person() 
        person.Name = "Mike" 
        person.Age = 20 
        person.Country = "United State" 
        person.Address = "Mike's address" 
        person.Mail = "" 
        person.Telephone = "13333333333" 
        person.Comment = "None" 
        Dim personTwo As New Person() 
        personTwo.Name = "James" 
        personTwo.Age = 22 
        personTwo.Country = "United State" 
        personTwo.Address = "James's address" 
        personTwo.Mail = "" 
        personTwo.Telephone = "13333333334" 
        personTwo.Comment = "Jim's comment" 
        Dim personThree As New Person() 
        personThree.Name = "Nancy" 
        personThree.Age = 21 
        personThree.Country = "China" 
        personThree.Address = "Nancy's address" 
        personThree.Mail = "" 
        personThree.Telephone = "13333333335" 
        personThree.Comment = "wang's comment" 
        Dim personFour As New Person() 
        personFour.Name = "Lisa" 
        personFour.Age = 28 
        personFour.Country = "United Kingdom" 
        personFour.Address = "Lisa's address" 
        personFour.Mail = "" 
        personFour.Telephone = "13333333336" 
        personFour.Comment = "li's comment" 
        Dim personFive As New Person() 
        personFive.Name = "Sakura" 
        personFive.Age = 24 
        personFive.Country = "Japan" 
        personFive.Address = "Sakura's address" 
        personFive.Mail = "" 
        personFive.Telephone = "13333333337" 
        personFive.Comment = "sa's comment" 
        Return _personList 
    End Function 
Step 8. Build the application and you can debug it.


MSDN: jQuery and Microsoft
MSDN: JSON Object (Windows Scripting - JScript)