在asp.net中调用JQuery中的服务器端方法

发布于 2024-08-09 01:55:11 字数 1270 浏览 9 评论 0原文

在我的应用程序中,我有一个表单,用户首先输入他们的用户名。现在我应该检查该用户名是否可用,我有一个方法“用户名”,该方法返回 true 或 false 作为返回类型。 在这里,我正在使用 jQuery 和 ajax 来实现这个概念。 一旦用户输入此名称,并且当他进入第二个文本框键入此代码时,应该执行此代码并将结果作为弹出窗口[moda popup] 提供给他。如果用户名已在使用中的方法“用户名”返回值为 true,则需要显示消息“用户名已在使用中” 如果返回值是 false“不需要显示”,

现在我的代码看起来像这样,

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

任何解决方案都会很棒 谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捶死心动 2024-08-16 01:55:11
<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>
<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文