1

Code Behind:

 [WebMethod]
        public static string emp()
        {
            return "BlaBla";
        }

Aspx Page:

$(document).ready(function() {

          $.get("TestPage.aspx/emp", null, function(data) {

                alert(data);

      })
     })

Message Box Output: TestPage.aspx on the page codes

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">    
<head><title>
</title>    
    <style>    
        tr    
        {
                background-color: red;
                color: White;    
        }
        </style>

How to make return string ?

Thank You.

1 Answer 1

3

use

$(document).ready(function() {
  // Add the page method call as an onclick handler for the div.
  $("#Result").click(function() {
    $.ajax({
      type: "POST",
      url: ""TestPage.aspx/emp",
      data: "{}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(msg) {
        // Replace the div's content with the page method's return.
        $("#Result").text(msg.d);
      }
    });
  });
});

In your page use

<div id="Result">Click here to return the string</div>

Sign up to request clarification or add additional context in comments.

6 Comments

The code behind method also needs to be static, and decorated with ScriptMethod as well as WebMethod.
@Pandiya Chendur, I Cant Run Your Code help
Then use jQuery instead, like Pandiya has shown. The important bit in the article is how your method in the code-behind file should be developed.
$("#Result").text(msg.d); Why is being used ".d" ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.