Gọi hàm từ server side bằng javascript trong asp.net Website

Gọi hàm từ server side bằng javascript trong asp.net Website

Gọi một hàm từ server side bằng javascript là một câu hỏi được rất nhiều bạn hỏi WEBXAULA với nhiều lý do muốn truy xuất dữ liệu, gọi session hay gọi một hàm đơn giản từ server side bằng javascript nhằm tránh load lại trang. 

Gọi hàm từ server side bằng javascript trong asp.net Website

WEBXAULA sẽ giới thiệu với các bạn cách sử dụng jquery để gọi hàm từ server side.  Ở đây tôi có một form gồm tên người dùng, email, mật khẩu và confirm mật khẩu, tôi sẽ lấy thông tin người dùng nhập và hiện lên form.

Các bạn chuẩn bị giao diện như sau: 

 

<div>
        <table border="0">
            <tr>
                <td>Username</td>
                <td><asp:TextBox ID="txtUsername" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td> Email</td>
                <td><asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Password</td>
                <td><asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Confirm</td>
                <td><asp:TextBox ID="txtConfirm" runat="server" TextMode="Password"></asp:TextBox></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="button" id="btnRegister" value="Submit" /></td>
            </tr>
        </table>
    </div>
    <div id="result" style="color:Red"></div>
 
Sau đó bạn add một webservice đặt tên là test và viết một hàm như sau:
 
[WebMethod]
    public string TestThu(string username, string email, string password, string confirm)
    {
       
            return "<b>Dữ liệu trả về</b> <br> Username : " + username + "<br>Email : " + email + "<br/>Mật khẩu: " + password;
           
    }

Bạn lưu ý dòng sau không được comment thì webservice mới có thể gọi được từ javascript:

// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
 [System.Web.Script.Services.ScriptService]

Quay lại code html, bạn gọi Jquery:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>

Sau đó bạn viết hàm javascript như sau:

<script type="text/javascript">
       $(document).ready(function () {
           // Add the page method call as an onclick handler for the div.
           $("#btnRegister").click(function () {
               var username = $("#txtUsername").val();
               var email = $("#txtEmail").val();
               var password = $("#txtPassword").val();
               var confirm = $("#txtConfirm").val();
 
               $.ajax({
                   type: "POST",
                   url: "test.asmx/TestThu",
                   data: "{username: " + username + ",email: " + email + ",password: " + password + ",confirm: " + confirm + "}",
                   contentType: "application/json; charset=utf-8",
                   dataType: "json",
                   success: function (msg) {
                       // Replace the divs content with the page methods return.
                       $("#result").html(msg.d);
                   }
               });
           });
       });
    </script> 

 

Bạn thấy bài viết này như thế nào?: 
Average: 10 (1 vote)
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Hướng dẫn sử dụng Display Suite and Views trong Drupal

Hướng dẫn sử dụng Display Suite and Views trong Drupal

Display Suite is one of our very favorite Drupal modules.

Facebook muốn thân với Apple "diệt" Google+?

Facebook muốn thân với Apple "diệt" Google+?

Mark Zuckerberg đã nhận ra điểm mạnh của Google+ chính là được tích hợp vào hệ điều hành Android, hệ điều hành di động phổ biến nhất thế giới hiện nay. Android đang phát triển không ngừng, đồng nghĩa với việc ngày càng có nhiều người sử dụng Google+ trên thị trường điện thoại di động. Điều này trở thành một mối đe dọa khôn lường cho Facebook.

Broad Match Modifier: từ khóa mở rộng bổ sung

Tìm hiểu về Broad Match Modifier

Chúng tôi đã có một bài viết giới thiệu 4 dạng từ khóa trong quảng cáo google adwords: từ khóa mở rộng, từ khóa cụm từ, từ khóa chính xác và từ khóa phủ định. Hôm nay, chúng tôi tiếp tục giới thiệu thêm một dạng từ khóa mới: từ khóa mở rộng bổ sung – Broad Match Modifier, từ khóa này có biên độ nằm giữa từ khóa mở rộng và từ khóa cụm từ.

Công ty diệt chuột T&C

 

Diet con trung