6 단계 ICallbackEventHandler 를 사용 하여 리 셋 없 는 리 셋 을 실현 합 니 다.

http://www.cnblogs.com/vcool/archive/2008/02/27/1083683.html
AJAX 기술 이 제창 하 는 리 셋 없 는 리 셋 은 원래 의 기술 에서 대량의 JavaScript 코드 를 쓰 거나 AJAX 프레임 워 크 를 사용 하여 개발 효율 과 유지 가능성 을 크게 낮 춰 야 한다.사실 ASP. NET 2.0 에 서 는 이미 이러한 인 터 페 이 스 를 제 공 했 는데 이것 이 바로 ICallbackEventHandler 이다.     ICallbackEvent Handler 인터넷 에 이미 많은 글 이 소개 되 었 는데, 이 글 은 사실 사족 을 그 리 는 것 이다.ICallbackEventHandler 는 System. Web. UI 에 존재 합 니 다. 아주 간단 한 예 를 들 어 사용 해 보 겠 습 니 다.    첫 번 째 단 계 는 VS 2005 에 새로운 WEB 창 을 만 듭 니 다.    두 번 째 단 계 는 ASPX 에 HTML 코드 를 올 립 니 다 (다음 과 같 습 니 다).
<body>
<form id="form1" runat="server">
<div>
<button onclick="CallServer()">CallServer</button>
</div>
</form>
</body>


세 번 째 단계, 그리고 < HEAD > < / HEAD > 에 자 바스 크 립 트 스 크 립 트 를 넣 습 니 다.
<script type="text/javascript">
function CallServer()
{
var product = "  ";
<%= ClientScript.GetCallbackEventReference(this, "product", "ReceiveServerData",null)%>;
}
function ReceiveServerData(rValue)
{
alert(rValue);
}
</script>

네 번 째 단 계 는 이 ASPX 의 배경 CS 코드 에서 ICallbackEventHandler 인 터 페 이 스 를 계승 하고 인터페이스 에 있 는 두 가지 방법 을 실현 합 니 다: ICallbackEventHandler. GetCallbackResult ()     ICallbackEventHandler. Raise CallbackEvent (string eventArgument)    다섯 번 째 단 계 는 변수 CallBackValue 를 추가 하고 인 터 페 이 스 를 수정 하 는 두 가지 방법 은 다음 과 같 습 니 다.
private string CallBackValue = string.Empty;
string ICallbackEventHandler.GetCallbackResult()
{
return CallBackValue + ",ok";
}
void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
{
this.CallBackValue = eventArgument;
}

 여섯 번 째 단계, 실행, 인터페이스 에 단추 가 나타 납 니 다. 클릭 하면 '테스트' 라 는 문자열 을 배경 으로 전달 합 니 다. 배경 C \ # 코드 는 문자열 에 ', OK' 를 추가 한 후 클 라 이언 트 의 자바 스 크 립 트 코드 에 되 돌려 표시 합 니 다.     이상 6 보 는 무 리 셋 리 셋 을 실현 할 수 있다.지금, 우 리 는 몇 단락 의 코드 를 분석 해 보 자.     세 번 째 단계 의 JavaScript 코드 를 먼저 보 세 요. 그 중의 CallServer () 방법 에서 리 셋 을 했 습 니 다. 리 셋 문 구 는 다음 과 같 습 니 다.
<%= ClientScript.GetCallbackEventReference(this, "product", "ReceiveServerData",null)%>;

이 네 개의 매개 변수 중 두 번 째 매개 변 수 는 produt 이라는 자바 스 크 립 트 의 문자열 변 수 를 배경 으로 전송 하 는 것 을 지정 합 니 다. 세 번 째 매개 변 수 는 배경 에서 돌아 올 때 되 돌아 오 는 정 보 를 받 는 자바 스 크 립 트 방법 ReceiveServerData (string Value) 를 지정 합 니 다.     다섯 번 째 단계 에서 배경 두 가지 방법 은 ICallbackEventHandler. RaiseCallbackEvent (string eventArgument) 가 프론트 JavaScript 에서 들 려 오 는 문자열 변 수 를 받 아들 이 고 내부 변수 this. CallBackValue 에 값 을 부여 합 니 다. 다른 방법 은 ICallbackEventHandler. GetCallbackResult ()변 경 된 내부 변 수 를 this. CallBackValue 를 프론트 자바 스 크 립 트 방법 ReceiveServerData (string Value) 에 되 돌려 줍 니 다.     호출 순 서 는: (프론트) CallServer () -- > (백 엔 드) ICallbackEventHandler. RaiseCallbackEvent (string eventArgument) -- > (백 엔 드) ICallbackEventHandler. GetCallbackResult () -- > (프론트) ReceiveServerData (string Value) 입 니 다.     전체 호출 과정 은 매우 간단 합 니 다. 그 중에서 매우 중요 한 단 계 는 세 번 째 단계 의 <% = Client Script 입 니 다. GetCallbackEventReference (this, "product", "ReceiveServerData", null)% > 입 니 다.이 방법 은 다음은 인터넷 에서 찾 아 온 자료 입 니 다. 여러분 이 보 실 수 있 습 니 다.GetCallbackEventReference 는 클 라 이언 트 요청 이 끝 날 때 클 라 이언 트 방법 을 회수 합 니 다.그것 도 CallBackManager 에 게 어떤 호출 방법 이 생 겼 는 지 확인 하 게 한다.이 예 에서 사용 하 는 재 부팅 방법 은:
public string GetCallbackEventReference(
string target, string argument,
string clientCallback, string  context,
string clientErrorCallback)

Table 1. GetCallBackEventReference 방법의 매개 변수 설명.Parameters Description target ID of the page where the callback invocation is handled. For more see the other overloaded options available in the next immediate section.In our sample "this" is the argument value, since the callback is handled in the same page.   argument This is the parameter defintion used to send value to the server. This value is received by parameter "eventArgument" at the server end using the RaiseCallbackEvent event."arg" becomes the first parameter name in our sample. The value is passed through this argument from the client. clientCallback Method name of the callback that is invoked after successful server call."CallBackHandler" is the method name that handles the callback.    context A parameter that is associated with the "argument" from the client. It usually should be used to identify the context of the call. You will understand this better from the sample implementation.In the sample "ctx"is just another parameter definition used. The value for this is passed from the client. client ErrorCallback Name of the method that is called from the CallBackManager in case of any errors. 이 방법 에서 돌아 온 string 은:
__doCallback('__Page',arg,CallBackHandler,ctx, ErrorCallBack)

또 다른 과부하 방법 은:
public string GetCallbackEventReference(
Control control, string argument,
string clientCallback, string  context)
public string GetCallbackEventReference(
Control control, string argument,
string clientCallback,  string  context,
string clientErrorCallback)

좋은 웹페이지 즐겨찾기