开发者

calling server side method in JQuery in asp.net

开发者 https://www.devze.com 2022-12-10 17:34 出处:网络
in my applicationi haveanformwhereuser entertheir usernamefirst . now i should checkwhetherthat username is avilableor not i havewtitten anmethod \"username\"which does thiswhuchreturntrueor falseasar

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() {    
开发者_JAVA技巧          $.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


<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>
0

精彩评论

暂无评论...
验证码 换一张
取 消