Showing posts with label ASP.Net with AJAX. Show all posts
Showing posts with label ASP.Net with AJAX. Show all posts

Sunday, April 2, 2017

Evolution of AJAX World


If you have started you carrier around 7-10 years back as web developer then probably you have experienced the evaluation practically.

Because of huge advantage of AJAX, more or less 70-80 % of modern web application has adapted this technology. The new style of AJAX implementation came in market so quickly that it’s sometime hard to remember what things were before that?

Anyway, Long ago, Netscape added a feature in browser called live script which was capable to give flavor of offline processing like form validation in client side and bit more. Slowly Live script became JavaScript with more functionality and flexibility.

The inception of DynamicHTML has started in development world, slowly XML gain its popularity in AJAX world when Microsoft added a small function in IE5 to perform XMLHttpRequest call to server.

The classic AJAX using XMLHttpRequest

This is classic style to make AJAX call using vanilla JavaScript. If you worked in very old ASP, JSP or PHP application, probably you are familiar with this style of AJAX call.

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
       // Typical action to be performed when the document is ready:
       document.getElementById("demo").innerHTML = xhttp.responseText;
    }
};
xhttp.open("GET", "yourfilename.extension", true);
xhttp.send();

Oh, did I miss something? Yes, we need to detect browser prior to AJAX call, Just to make sure we are using proper XMLHttpRequest object. Yes, XMLHttpRequest object is browser specific.

JQuery changed the whole game

Then JQuery came in market, it played major role to change face of client side app development.  It combine and packed the above code into few lines and introduce the concept of Callback.

$.ajax({url: "yoururl.com", success: function(result){
        //enjoy with result variable
    }});

And that’s all, simple, compact, no code smell and maintainable. Now, in mean time other library too started wrapping classic XMLHttpRequest request using very similar syntax.

So, as a result the concept of callback has introduced in development world. The moto of callback is, call me once you have prepared the result, or call error in mess!.

Let’s return promise rather than actual result

People started to realize that, as it is asynchronous call, it should return something by which we can take decision. Let’s give some example. The below code is based on promise and then callback function.

requestSomeData("http://example.com ") // returns a promise for the response

    .then(function(response){ // ‘then’ is used to provide a promise handler
    
    return JSON.parse(response.body); // parse the body
    
   }) // returns a promise for the parsed body

    .then(function(data){
   
    return data.something; // get the price

   }) // returns a promise for the price

    .then(function(price){ // print out the price when it is fulfilled

        //do something with the data

  });

We can consider this as fully asynchronous execution plan, even if my JSON.parse() take huge time, the execution flow will not hang the thread because I have attached then callback to ensure that someone will take care once parse completed.

Angular and Other library utilize it best

Angular 1.X introduced a way to sync all asyn call using “$q” service. This is huge power to developer to process more than one asynchronous call and print consolidated result.

Want to have more control on AJAX call? Use Observable.

RxJs introduced the concept called Observable to have more grip on AJAX call. Let’s think that we need to cancel an AJAX call in middle. Assume, the situation where need to handle multiple AJAX call. Observable is preferred than promise here.

So, an observable is like a stream and allow to pass zero or more events where the callback is called for each event. The Observable covers more including promise.
For more info, please check Observable in Angular2

    









Saturday, March 10, 2012

ASP.Net AJAX rounded shape area


This is the example of ASP.Net AJAX rounded corner extender. Paste below code in your ASP.Net AJAX enable web page.



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="toggle_button.aspx.cs" Inherits="toggle_button" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager runat="server" ID="Scrptman">
    </asp:ScriptManager>
    <div align="center">
    <table width="50%" >
    <tr align="center">
   
    <td valign="middle"><asp:Panel BackColor="BlueViolet" Height="200px"
        runat="server" ID="MainPanel" Width="50%" HorizontalAlign="Center">
   <table align="center" style="height:100px;">
  
   <tr>
   <td align="center" valign="middle">
     This is ASP.Net AJAX rounded corner extender
  
   </td></tr></table>
   </asp:Panel>
   
    </td></tr></table>
   
     <cc1:RoundedCornersExtender Corners="All" Radius="10" TargetControlID="MainPanel"
          ID="RoundedCornersExtender1" runat="server"></cc1:RoundedCornersExtender>
    </div>
    </form>
 
</body>
</html>

ASP.Net AJAX application to check use avability


This program is for check whether a username is already taken or not. Copy and paste below code into a AJAX enable ASP.Net page and edit database connection section according to your database setting.



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="user_check.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
   
<style type="text/css">
.waitingdiv {
background-color: #F5F8FA;
border: 1px solid #5A768E;
color: #333333;
font-size: 93%;
margin-bottom: 1em;
margin-top: 0.2em;
padding: 8px 12px;
width: 8.4em;
}
</style>
   
   
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="scriptmanager1" runat="server">
    </asp:ScriptManager>

<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
function BeginRequestHandler(sender, args) {
var state = document.getElementById('loadingdiv').style.display;
if (state == 'block') {
document.getElementById('loadingdiv').style.display = 'none';
} else {
document.getElementById('loadingdiv').style.display = 'block';
}
args.get_postBackElement().disabled = true;
}
</script>


<asp:UpdatePanel ID="PnlUsrDetails" runat="server">
<ContentTemplate>
<table>
<tr>
<td>
UserName:
</td>
<td>
<asp:TextBox ID="txtUsername" runat="server" AutoPostBack="true" ontextchanged="txtUsername_TextChanged"/>
</td>
<td>
<div id="checkusername" runat="server"  visible="false">
<asp:Image ID="imgstatus" runat="server" Width="17px" Height="17px"/>
<asp:Label ID="lblStatus" runat="server"></asp:Label>
</div>
</td>
</tr>
</table>
<div class="waitingdiv" id="loadingdiv" style="display:none; margin-left:5.3em">
<img src="LoaderClockIcon.gif" alt="Loading" style="height: 30px" />Please wait...
</div>
</ContentTemplate>
</asp:UpdatePanel>

</form>
</body>
</html>
 

Moveable panel using ASP.Net AJAX


This is the example of moveable  panel using AJAX. After clicking login button one login screen will come and you can place the panel any where in your page. Open a AJAX enable ASP.Net page and paste the below code.





<%@ Page Language="C#" AutoEventWireup="true" CodeFile="change_location.aspx.cs" Inherits="change_location" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <style type="text/css">
.Dialog {
background: none repeat scroll 0 0 #E5E8D2;
border: 1px solid #466D77;
}
body {
font-family: Verdana,Tahoma,Arial !important;
font-size: 9pt;
}
.TitleBar {
background: url("images/titleBarBg.gif") repeat-x scroll left bottom #FAFAFA;
cursor: move;

}
.Button {
background: #E9E9E9;
border: 1px solid #9B9B9B;
padding: 0.1em;
vertical-align: middle;
}
.modalPopup
{
background-color: #696969;
filter: alpha(opacity=40);
opacity: 0.7;
xindex:-1;
}
</style>
   
   
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager> 
   
    <ajax:DragPanelExtender ID="dragpnl" runat="server" TargetControlID="pnllogin" DragHandleID="pnldrag"/>
<ajax:ModalPopupExtender ID="modalpopup1" runat="server" BackgroundCssClass="modalPopup" TargetControlID="btnshow" PopupControlID="pnllogin"/>
<asp:Button ID="btnshow" runat="server" Text="Show Login" />
<asp:Panel ID="pnllogin" runat="server">
<div style="width: 400px">
<asp:Panel ID="pnldrag" runat="server">
<table class="Dialog" width="100%">
<tr style= " background-image:url(bg_filter_header.gif)" class="TitleBar">
<td style="color:Black">
<img src="cross.jpg" align="right" style="cursor:pointer" alt="Close" onclick="ClosePopup()"/> Enter Login Details</td>
</tr>
<tr>
<td>
<table width="100%" border="0" align="center" style="">
<!-- login id -->
<tr>
<td align="right">User ID:</td>
<td align="left">
<asp:TextBox ID="txtUsername" runat="server" OnTextChanged="txtUsername_TextChanged"></asp:TextBox>
</td>
</tr>
<!-- password -->
<tr>
<td align="right">Password:</td>
<td align="left">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnsubmit" runat="server" Text="Submit" CssClass="Button" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" CssClass="Button"  />
</td>                            
</tr>
<tr>
<td align="right">
<input type="checkbox" />
</td>
<td align="left">Remember User ID</td>                            
</tr>
</table>
</td>
</tr>
</table>
</asp:Panel>
</div>
</asp:Panel>
<script type="text/javascript">
function setBodyContentHeight() {
//Setting height of body to maintain position of drag panel
document.body.style.height = Math.max(document.documentElement.scrollHeight,document.body.scrollHeight) + "px";
}
setBodyContentHeight();
function ClosePopup() {
//Hide the modal popup - the update progress
var popup = $find('<%= modalpopup1.ClientID %>');
if (popup != null) {
popup.hide();
}
}
</script> 

 
    </div>
    </form>
</body>
</html>