views:

311

answers:

1

hi, in my application i have an form where user enter their username first . now i should check whether that username is avilable or not i have wtitten an method "username" which does this whuch return true or false as a return type. here i am doing using jQuery with ajax to achive this concept. once the user enter this name and when he goes for the second textbox to type this code should get executed and give him the result as a pop up[moda popup] . if return value is true from method "username" from user name is already in use need to display message "username alredy in use" if return value is false "no need to display"

right now my code looks like this

<head>
  <title>Calling an ASP.NET Page Method with jQuery</title>
  <script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<script type="text/javascript">

      $(document).ready(function() {    
          $.ajax({    
              type: "POST",    
              url: "Default.aspx/Username",    
              contentType: "application/json; charset=utf-8",    
              data: "{}",    
              dataType: "json",    
              success: OnSuccess,    
              error: OnFailure    
          });    
    });

      function OnSuccess(result) 
      {
         // so  here i need   to  check  whethere  true  or false
         // based on that i need   to  show  modal  pop  up
          alert("Success!");    
      }

      function OnFailure (result)    
      {
          alert("The call to the page method failed.");    
      }    
  </script>    
</head>

any solution on this would be great thank you

+2  A: 
<asp:TextBox id="txtUserName" runat="server"/>
<div id="divPrompt" style="display:none">User Name alredy in use</div>
<input id="otherText"...../>

<script type="text/javascript">
$(document).ready(function(){
    $("#<%= txtUserName.ClientID%>").blur(function(){
       $.ajax({    
              type: "POST",    
              url: "Default.aspx/Username",    
              contentType: "application/json; charset=utf-8",    
              data: "{}",    
              dataType: "json",    
              success: function (msg){
                  if(msg.hasOwnProperty("d")){
                     OnSuccess(msg.d);
                  } else{
                     OnSuccess(msg);
                  }
              },
              error: OnFailure
          });    
    });
});

  function OnSuccess(result) 
  {
     if(result.UserNameInUser)
       $("div#divPrompt").show();
     else
       $("div#divPrompt").hide();
  }

  function OnFailure (result)    
  {
      alert("The call to the page method failed.");    
  }    
</script>
TheVillageIdiot