博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Net 2.0 CallBack 流程

Posted on 2006-11-03 10:42  自適應軟件......  阅读(1758)  评论(6编辑  收藏  举报


1.首先繼承  ICallbackEventHandler
2.在Page_Load 裡面 

            ClientScriptManager scriptManager = this.Page.ClientScript;
            string eventRef = scriptManager.GetCallbackEventReference(this, "argVal", "OnCallComplete", "ctxVal", "OnErrorOccurs", true);


3.Server:


    /// <summary>
    /// 接受客戶端的參數 進行運算
    /// </summary>
    /// <param name="eventArgument"></param>
    public void RaiseCallbackEvent(string eventArgument)
    {
        strCallResult = voidtt();
    }


     /// <summary>
    /// 返回CallBack的結果
    /// </summary>
    /// <returns></returns>
    public string GetCallbackResult()
    {
        return strCallResult;
    }


4.For Example:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>Untitled Page</title>
    
    
<script type="text/javascript">
        
var value_1="";
        
        
function tt()
        
{
            
//doOther();
        }

        
        
function ProcessCallBackError(arg, context)
        
{
           alert('error
!');
        }

    
</script>
</head>

<body>
    
<form id="form1" runat="server">
    
<div>
        
<span id="Message_1"></span>
        
<input type="button" value="tt" onclick="callServerData(value_1,tt)" />
    
    
</div>
    
</form>
</body>
</html>



    public string outputCount = "";
    
protected void Page_Load(object sender, EventArgs e)
    
{   

        
if (!IsPostBack)
        
{ }

        ClientScriptManager csm 
= Page.ClientScript;
        StringBuilder context 
= new StringBuilder();

        context.Append(
"function ReturnServerValue(arg, context)");
        context.Append(
"{");
        context.Append(
"Message_1.innerText = arg; ");
        context.Append(
"value_1 = arg;");
        context.Append(
"}");

        String callbackReference 
= csm.GetCallbackEventReference(this"arg""ReturnServerValue", context.ToString());
       
        String callbackspt 
= "function callServerData(arg,context) {" + callbackReference + ";}";

        csm.RegisterClientScriptBlock(
this.GetType(), "callServerData",callbackspt,true);
    }



    
/// <summary>
    
/// 接受客戶端傳回的參數
    
/// </summary>
    
/// <param name="eventArgument"></param>

    public void RaiseCallbackEvent(String eventArgument)
    
{
        
try
        
{
            outputCount 
= eventArgument.ToString();
        }

        
catch
        
{
            outputCount 
= "output";
        }

    }


    
/// <summary>
    
/// Define method that returns callback result.(將運算好的結果返回)
    
/// </summary>
    
/// <returns></returns>

    public string GetCallbackResult()
    
{
        
return outputCount;
    }