기본적으로는 서버와 클라이언트 코드는 서로를 직접적으로 호출 할 수 없다. 서로 실행되는 시간과 위치가 다르기 때문이다.

서버코드 : Requeset를 받았을때 서버에서 실행

클라이언트코드 : Response를 받은 클라이언트 브라우저에서 실행

 

하지만, 이를 가능하게 하는 몇가지 방법이 있고, 매번 기억하기 어려워서 여기에 정리해본다.

뭐 고급유저라면 관련 Tip을 상당히 여러가지 알고 있겠지만, 그래도 모든걸 항상 기억하긴 여러우니까...

<작성: http://blog.naver.com/myfancy>

 

1. ASP.NET AJAX - Web Service 호출

스크립트에서 Web Service 컨테이너에서 노출하고 있는 함수를 AJAX로 호출하는 방법이다. 포스트백이나 페이지 리프레시가 발생하지 않는다. 하지만, 비교적 많은 이해가 필요하고, .asmx로 웹서비스를 별도로 만들어야만 된다.

참고) Client-Side Web Service Call with AJAX Extentions

         http://msdn.microsoft.com/en-us/magazine/cc163499.aspx

        Calling Web Servivce from Client Script

         http://msdn.microsoft.com/en-us/library/bb398995.aspx

         http://msdn.microsoft.com/ko-kr/library/bb398998.aspx

 

2. ASP.NET Ajax Extentions - PageMethod 호출

PageMethod는 기본적으로 Page의 Behind코드에 public static으로 노출하고, JavaScript에서 호출하는 방식이다. PageMethod는 [WebMethod]라고 함수에 꾸며주기만 하면되고, Response에 Inline JavaScript Proxy 코드가 생성된다.

 

[System.Web.Services.WebMethod]
public static string GetContactName(string custid){}
함수는 반드시 public static이여야 한다.
 

<asp:ScriptManager ... EnablePageMethods=”true”/>

이 기법을 사용하기 위해서는 ScriptManager에 EnablePageMethods속성을 true로 설정하면 된다.

 

function CallGetContactName(custid,cbParam)
{    
 // call server side method
 PageMethods.GetContactName(custid,OnSuccess,OnFailed,cbParam);
}
// set the destination textbox value with the ContactName
function OnSuccess(res, cbParam)
{    
 //결과값 : res, 콜백파라미터:cbParam
}
// alert message on some failure
function OnFailed(res, cbParam)
{
 //결과값 : res, 콜백파라미터:cbParam
 alert(res.get_message());
}
스크립트를 별도의 파일로 추가할 경우 <head/>섹션이 아닌 <body>태그 아래 추가해야 한다.

3. jQuery를 이용한 WebService 호출

http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/

http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/

http://encosia.com/2008/06/05/3-mistakes-to-avoid-when-using-jquery-with-aspnet-ajax/

 

4. Get, Post를 이용한 방법 (Non Ajax)

ASP.NET의 Page, HttpHandler, HttpModule등을 이용하여 Request를 받아 Response를 주는 인스턴스를 만들어 놓고, 순수 Ajax나, Post등의 기법을 이용하여 처리 할 수 있다.


※ VS 2008 다운로드 

http://www.asp.net/downloads/essential/




※ ASP.NET AJAX

  •  Watch the ASP.NET AJAX Support in VS2008 Video
  •  Watch the Adding AJAX Functionality to an Existing ASP.NET Page Video
  •  Watch the Creating and Using an AJAX-enabled Web Service in a Web Site Video
  • The ASP.NET AJAX Site
  • ASP.NET AJAX Overview 




    ※ New ListView and DataPager Controls
  •  Watch the ListView Control Video
  •  Watch the DataPage Control Video
  • Using the ASP.NET 3.5 ListView Control
  • ListView Web Server Control 




    ※ LINQ and other .NET Framework 3.5 Improvements
  •  Watch the LINQ How Do I Video Series
  • Part 1: Introduction to LINQ and SQL
  • Part 2: Defining our Data Model Classes
  • Part 3: Querying our Database
  • Part 4: Updating our Database
  • Part 5: Binding UI using the ASP:LinqDataSource Control
  • Part 6: Retrieving Data Using Stored Procedures
  • Part 7: Updating our Database using Stored Procedures
  • Part 8: Executing Custom SQL Expressions
  • Part 9: Using a Custom LINQ Expression with the <asp:LinqDataSource> control
  • LinqDataSource Technology Overview 




    ※ LINQ and other .NET Framework 3.5 Improvements

    Using ASP.NET Web Services Roadmap 




    ※ New Web Design Interface
  •  Watch the New Designer Support in VS2008 Video
  •  Watch the Quick Tour of the VS2008 IDE Video
  •  Watch the VS2008 and Nested Masterpages Video
  •  Watch the Creating and Modifying a CSS File Video
  • VS 2008 Web Designer and CSS Support
  • VS 2008 Nested Master Page Support
  • Working with CSS Overview





    ※ JavaScript Debugging and Intellisense

  •  Watch the JavaScript Intellisense Support in VS2008 Video
  •  Watch the JavaScript Debugging in VS2008 Video
  •  Watch the IntelliSense for JScript and ASP.NET AJAX Video
  • Visual Studio 2008 JavaScript Debugging
  • Visual Studio 2008 JavaScript Intellisense 




    ※ Multi-targeting Support
  •  Watch the Multi-Targeting Support in VS2008 Video
  • Visual Studio 2008 Multi-Targeting Support
  • .NET Framework Multi-targeting Overview




    ※ Other Resources

  • ASP.NET AJAX Forums
  • Visual Studio 2008 Forum
  • Visual Web Developer 2008 Express Forum

  • + Recent posts