Asynchronous Request to Server (JavaScript)




I this post i will show how to send Asynchronous Request to Server which will Query the Database for certain information. Here, I am giving one example, which will send an Asynchronous Request to server for Checking the availablity of the Customerid by the user against database.

Step1.Create a new page(Async.aspx) and write down following code

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

<!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>
<script type="text/javascript">
function CheckUserNameAvailablity1()
{
var name=document.getElementById('<%=TextBox1.ClientID%>').value;
CheckUserNameAvailablity2(name);
return false;
}
function CheckUserNameAvailablity2(Name)
{
try
{
var Source = new ActiveXObject("Microsoft.XMLDOM");
var XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
var ElemList;
var Request = "DOM.aspx?Name=" + Name;
XmlHttp.open("POST",Request,false);
XmlHttp.send();
Source.async = false;
if (Source.loadXML(XmlHttp.responseText) == true)
{
ElemList = Source.getElementsByTagName("tblAjaxEx");
if(ElemList.length > 0)
alert("Entered USerName is already Exists..");
else
alert("Entered UserName is available..");
}
}
catch(Ex)
{
alert(Ex.message);
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="UserName:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return CheckUserNameAvailablity1()" />
</div>
</form>
</body>
</html>

Step2.Create a second Page Dom.aspx and remove all the html except Page attribute and write down the following code

DOM.aspx



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



DOM.aspx.cs



using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class DOM : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
CheckUserNameAvailablity(Convert.ToString(Request.QueryString["Name"]).ToUpper());
}
}
private void CheckUserNameAvailablity(string Name)
{
DataSet dsDataSet = new DataSet();
string strSQL = "Select customerid From customers Where Upper(customerid) = '" + Name + "'";
SqlConnection cn = new SqlConnection("server=(local);database=northwind;uid=sa;pwd=12345");
cn.Open();
SqlDataAdapter adAdapter = new SqlDataAdapter(strSQL, cn);
adAdapter.Fill(dsDataSet, "tblAjaxEx");
Response.Clear();
if (dsDataSet != null)
dsDataSet.WriteXml(Response.OutputStream);
Response.End();
}
}

No comments:

Post a Comment