SIGN UP MEMBER LOGIN:    
ARTICLE

Required Field Validator in AJAX

Posted by Mahak Gupta Articles | AJAX in C# November 13, 2010
In this article I'll show you how to call a RequiredFieldValidator with the help of AJAX.
Reader Level:


In this article I'll show you how to call a RequiredFieldValidator with the help of AJAX.

Step1:- In my case,there are three controls in my .aspx page

  1. TextBox (txtfname).

  2. Label (lblmsg) :-To show the Error Message.

  3. And a Button (btnshow).

alax.gif


<asp:TextBox ID="txtfname" runat="server"></asp:TextBox>
 <asp:Label ID="lblmsg" runat="server" Text="Please Enter your First Name" Style="color: #ff0000; display:none;"></asp:Label>
   <input id="btnshow" type="button" onclick="ShowValdidator()" value="Enter" />


Note:- Here We set the style of Label (lblmsg) :- display:none :-means the label is not displayed.

Here We call the function ShowValdidator() on the onclick event of btnshow, by this we check the TextBox value and call the Label (error meassage)

Step 2: Call the Function (ShowValdidator()):

In the <head> part write the folowing code:-

<script language="JavaScript" type="text/javascript" >

var xmlHttp

var arr;

function ShowValdidator()

{

      xmlHttp=GetXmlHttpObject()

      var url="Default.aspx"

      url=url+"?fname="+document.getElementById('txtfname').value

      xmlHttp.onreadystatechange=stateChanged

      xmlHttp.open("GET",url,true)

      xmlHttp.send(null)

      return false;

}

function stateChanged()
 
{
 
      if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 
      {
 
      var str;
 
      str = xmlHttp.responseText;
 
      if(str=="")
      {
        document.getElementById("lblmsg").style.display='';
 
      }
      else
      {
         document.getElementById("lblmsg").style.display='none';
      }
      }

}     

function GetXmlHttpObject()
 
{
 
      var objXMLHttp=null
      if (window.XMLHttpRequest)
      {
      objXMLHttp=new XMLHttpRequest()
      }
      else if (window.ActiveXObject)
      {
      objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
      }
      return objXMLHttp
}

 </script>


Step 3:- In he .cs page:-

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        if (Request.QueryString["fname"] != null)
        {
            string fname = Request.QueryString["fname"];
            Response.Clear();
            string str = fname.ToString();
            Response.Write(str);
            Response.End();
        }
    }
}

Login to add your contents and source code to this article
share this article :
post comment
 

article title is not right. change it.

Posted by Raj Kumar Nov 13, 2010
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor