tags:

views:

56

answers:

1

Hi all;

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"&gt;

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

How to make return string ?

Thank You.

+2  A: 

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>

Pandiya Chendur
The code behind method also needs to be static, and decorated with `ScriptMethod` as well as `WebMethod`.
Andy Shellam
http://geekswithblogs.net/frankw/archive/2008/03/13/asp.net-ajax-callbacks-to-web-methods-in-aspx-pages.aspx
Andy Shellam
@Pandiya Chendur, I Cant Run Your Code help
Oraclee
@Andy Shellam, I don't want scriptmanager
Oraclee
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.
Andy Shellam
I can run, Im sorry my error
Oraclee
$("#Result").text(msg.d); Why is being used ".d" ?
Oraclee
AFAIK .d is json style
Sharique
@Sharique ,detail tell pls ,I do not understand
Oraclee
@oraclee http://api.jquery.com/text/
Pandiya Chendur