| 介面 | 方法 | 職責 |
|---|---|---|
ICallbackEventHandler | RaiseCallbackEvent(string eventArgument) | 伺服器端處理回調邏輯,回傳結果字串 |
GetCallbackResult() | 回傳給客戶端的最終字串 | |
ICallbackContainer | GetCallbackScript(IButtonControl, string) | 產生觸發回調的客戶端 JS 調用代碼 |
public class CallbackValidator : WebControl, INamingContainer, ICallbackContainer, ICallbackEventHandler
{
// ICallbackEventHandler
public void RaiseCallbackEvent(string eventArgument) { /* 驗證邏輯 */ }
public string GetCallbackResult() { return validationResultJson; }
// ICallbackContainer
public string GetCallbackScript(IButtonControl buttonControl, string argument)
{
return Page.ClientScript.GetCallbackEventReference(this, argument, "ReceiveServerData", "context");
}
}
function ReceiveServerData(result, context) { /* 解析 JSON 更新 UI */ }
<input type="button" onclick="<%= callbackValidator.GetCallbackScript(...) %>" />
ICallbackEventHandler。GetCallbackEventReference 產生標準回調調用,包含引數、回呼函數名、context。掌握此模式,即可開發如「即時驗證」、「增量載入」、「即時搜尋建議」等豐富互動控件。