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

 
Video: Installing Drush on Mac OS X

Video cài đặt Drush trên Mac OS X

Last weekend we made a video that shows, hopefully, how easy it is to install Drush, the fabulously powerful command line tool that can drastically speed up developing, installing and maintaining Drupal sites. If you're worried about command line, don't fret. You can type, right? Then you can do command line!

7 kinh nghiệm  thiết kế web trên mobile

7 kinh nghiệm thiết kế web trên mobile

Internet di động tiếp tục phát triển rất nhanh chóng. Những hướng dẫn này sẽ giúp bạn tạo ra hoặc cải thiện trải nghiệm người sử dụng điện thoại di động của bạn.

Dùng Facebook để quảng cáo, tiếp thị trực tuyến

Dùng Facebook để quảng cáo, tiếp thị trực tuyến

Khi quyết định sử dụng một kênh quảng cáo tiếp thị nào, ta cần phải đánh giá hiệu quả thu được dự kiến dựa trên các con số thực tế. Bài viết này sẽ đưa ra một số thông tin cụ thể hơn nhằm giúp các bạn nắm được các thông tin cơ bản về lượng người dùng của Facebook.

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

 

Diet con trung