Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Asynchronous update of page immediately after load to improve response time

0.00/5 (No votes)
14 Mar 2011 1  
Response time is one of major factor in deciding the performance of web application. Many time we come across pages where content of page can be

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Response time is one of major factor in deciding the performance of web application. Many time we come across pages where content of page can be updated asynchronously immediatly after the page rendering in responce to page request.

We can achive this using ajax call to server immediatly after the page load. In this article we will update the grid after the page load using ajax.

<form id="form1" runat="server"> 
   
<div> 
     
<asp:GridView ID="gvEmployee" runat="server" AutoGenerateColumns="false">          
         
<Columns> 
               
<asp:TemplateField> 

                    <HeaderTemplate> 
                       
<h3>Name</h3> 
                   
</HeaderTemplate> 
                   
<ItemTemplate> 
                        <asp:Label ID="name" runat="server" Text='
<%# Eval("name") %>' class="name"></asp:Label> 
                   
</ItemTemplate> 
               
</asp:TemplateField> 
 
               
<asp:TemplateField> 
                 
<HeaderTemplate> 
                       
<h3>Address</h3> 
                   
</HeaderTemplate> 
                   
<ItemTemplate> 
                       
<asp:Label ID="address" runat="server" Text="" class="address"></asp:Label> 
                   
</ItemTemplate> 
               
</asp:TemplateField> 
         
</Columns> 
     
</asp:GridView> 
   
</div> 
   
<script type="text/javascript"> 
        $
(function () { 
            $
(".name").each(function () { 
               
var lblNameObj = this 
                $
.ajax({ 
                    url
: "UpdateTableWithAjax.aspx/GetAddress", 
                    type
: "POST", 
                    contentType
: "application/json", 
                    dataType
: "json", 
                    data
: "{name : '" + this.innerHTML  + "'}", 
                    success
: function (msg) {//On Successfull service call 
                        $
("#" + lblNameObj.id).parent().next().children().html(msg.d) 
                   
}, 
                    error
: function (xhr, msg) { 
                        alert
(msg); 
                   
} 
 
               
}); 
           
}); 
 
       
}); 
 
   
</script> 
   
</form>

 

 protected void Page_Load(object sender, EventArgs e) 
        { 
            List<ClsEmployee> lstEmp = new List<ClsEmployee>(); 
            ClsEmployee emp1 = new ClsEmployee(); 
            emp1.name="Emp1"; 
            lstEmp.Add(emp1); 
 
            ClsEmployee emp2 = new ClsEmployee(); 
            emp2.name="Emp2"; 
            lstEmp.Add(emp2); 
 
            ClsEmployee emp3 = new ClsEmployee(); 
            emp3.name="Emp3"; 
            lstEmp.Add(emp3); 
 
            gvEmployee.DataSource = lstEmp; 
            gvEmployee.DataBind(); 
        } 
 
        [WebMethod] 
        public static string GetAddress(string name) 
        { 
            string adr=""; 
            //Loop has been added for the delay  
            for (int i = 0; i < 10000; i++) 
            { 
                for (int j = 0; j < 5000; j++) 
                { 
                    adr = "Address of " + name; 
                } 
            } 
 
             
            return adr; 
        } 

  public class ClsEmployee 
    { 
        public string name { get; set; } 
        public string address { get; set; } 
    }

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here