How To Use ASP.NET and HTTPrequest POST method

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

<!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 validateuserid(suserid) {
  document.body.style.cursor = 'wait';
  // Create an instance of the XML HTTP Request object
  var oXMLHTTP = new ActiveXObject( "Microsoft.XMLHTTP" );
  //debugger;
  // Prepare the XMLHTTP object for a HTTP POST to our validation ASP.net page
  var sURL = "http://localhost/MyPractice/check.aspx";
  oXMLHTTP.open( "POST", sURL, false );
  oXMLHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  // Execute the request
  oXMLHTTP.send("userID=" + suserid);
  //alert(oXMLHTTP.responseText);
  if (oXMLHTTP.responseText == "exists")
   document.getElementById("test").innerText = "Sorry - the User ID already exists.";
  else
  document.getElementById("test").innerText = "the User ID is available.";
  }
</script>
</head>
<body>
   <form id="form1" runat="server">
       <div>
           <input type="text" name="userid" value="" onchange="validateuserid(this.value)">
           <span id="test"></span>
       </div>
     
   </form>
</body>
</html>
Check.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Check.aspx.cs" Inherits="Test" %>

Check.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;

public partial class Test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string sUserID = Request.Form["userID"].ToString();
        if (sUserID == "Me")
        {
            Response.Write("exists");
        }
    }
}

No comments:

Post a Comment