ICallBackEventHandler

The important reference :

    The simple sample from the MSND: http://msdn.microsoft.com/zh-cn/library/ms178210(VS.80).aspx

    The six step to use the ICallBackEventHandler:http://www.cnblogs.com/soso/archive/2006/06/08/420687.html

    The mechanism of the ICallBackEvenHandler: http://hi.baidu.com/%CB%AE%C4%BE%C8%FD%C0%C9/blog/item/788da6462d947e016b63e5bf.html

    The deep analyze the  ICallBackEvenHandler:http://www.bczhe.com/web/ASP_NET/asp_net_4940.html

 

What we should pay more attentions:

  1.Why and when we should use the interface.

  2.The event occurs sequence.

  3.How they communicate

  4.The para "context" usage. Very good Aticle 菜鸟学 RaiseCallbackEvent GetCallbackResult GetCallbackEventReference

  4.Any other replace way.

 

The code for page and behind code:

 

Page Code
<% @ Page Language = " C# "  AutoEventWireup = " true "  CodeBehind = " ICallBackEventHandlerTest.aspx.cs "
    Inherits
= " WebApplicationTest.ICallBackEventHandlerTest "   %>

<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
< html xmlns = " http://www.w3.org/1999/xhtml " >
< head runat = " server " >
    
< title > Untitled Page </ title >
</ head >
< body >
    
< form id = " form1 "  runat = " server " >
    
< div >
        
< asp:ListBox runat = " server "  ID = " listBoxCatalog " ></ asp:ListBox >
        
< br  />
        
< br  />
        
< button type = " button "  id = " btnGetCatalog "  onclick = " GetSelectedCatalog() " > Get Selected catalog value </ button >
        
< br  />
        
< br  />
        
< label id = " labelCatalogValue " >
            The selected catalog value:
        
</ label >
    
</ div >
    
</ form >

    
< script type = " text/javascript " >
    
    var preFix
= " The selected catalog value: " ;
    
    var Val 
= " The value of the context param " ;
    
// call back to the server
    function GetSelectedCatalog()
    {
        var listBox 
=  document .getElementById ( " listBoxCatalog " );
        var catalog 
=  listBox.value;
        CallServer(catalog ,Val);    
    }    
    
    
    
//  1.function ReceiveServerData(retValue) will has the same result
    
//  2.use the context param you can get the "hahhaha" passed by CallServer(catalog ,"hahhaha");
    
//  3.if you have define the js function that in the GetCallbackEventReference(this, "arg", "ReceiveServerData", some js function string)
    
//    this function will not be excuted. 
    
//  4. this function receives the data and consumes the call back result data
    function ReceiveServerData(retValue,context)
    {   
        var labelCatalog
=  document.getElementById( " labelCatalogValue " );
        labelCatalog .innerText 
= preFix  +  retValue ;   
        alert (context);                  
    } 
    
    
    
    
</ script >

</ body >
</ html >

 

 Behind Code
using  System;
using  System.Collections;
using  System.Configuration;
using  System.Data;
using  System.Linq;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.HtmlControls;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Xml.Linq;
using  System.Text;

namespace  WebApplicationTest
{
    
public   partial   class  ICallBackEventHandlerTest : System.Web.UI.Page, ICallbackEventHandler
    {

        
protected  System.Collections.Specialized.ListDictionary catalogs;
        
string  selectedCatalog;

        
protected   void  Page_Load( object  sender, EventArgs e)
        {
           

            
// 1. if the fourth param is js function, the page defined js function that hanles the call back result data will not be excuted
            
//    else the fourth param should be the same as the second param name in "function CallServer(arg,context)"
            
// 2. And the second param name should be the same as the first param in the ""function CallServer(arg,contex)")"
            
//    the reason is the final js function is:  
            
//     function CallServer(arg,context)
            
//     {  
            
//        WebForm_DoCallback('__Page',arg,ReceiveServerData,context,null,false),
            
//     }  
          
            
// the result :WebForm_DoCallback('__Page',arg,ReceiveServerData,context,null,false),
            
// string cbeRe = ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData","context");            
             string  cbeRe  =  ClientScript.GetCallbackEventReference( this " arg " " ReceiveServerData " , GetHandleBackDataScript());
            StringBuilder cbScript 
=   new  StringBuilder();
            cbScript.Append(
" function CallServer(arg,context) " );
            cbScript.AppendLine(
" { " );
            cbScript.AppendLine(cbeRe);         
            cbScript.AppendLine(
" } " );

            
// register the js to page
             this .Page.ClientScript.RegisterClientScriptBlock( this .Page.GetType(),  " CallServer " , cbScript.ToString(),  true );

            
// get the Catalogs
            GetCatalogs();

            
// Bind Data
            BindDatas();
        }

       

        
public   string  GetCallbackResult()
        {
            
return  selectedCatalog;
        }

        
public   void  RaiseCallbackEvent( string  eventArgument)
        {
            
// set the selectedCatalog value
             if  (catalogs[eventArgument]  ==   null )
            {
                selectedCatalog 
=   " no catalog selected " ;
            }
            
else
            {
                selectedCatalog 
=  catalogs[eventArgument].ToString();
            }
        }

        
///   <summary>
        
///  Gets the js string that handles the call back data.
        
///  
        
///   </summary>
        
///   <returns></returns>
         string  GetHandleBackDataScript()
        {
            StringBuilder ClienScript 
=   new  StringBuilder();
            ClienScript.AppendLine(
"  function ReceiveServerData(retValue,context) " );
            ClienScript.AppendLine(
" { " );
            ClienScript.AppendLine(
"     var labelCatalog= document.getElementById(\ " labelCatalogValue\ " ); " );
            ClienScript.AppendLine(
"     labelCatalog .innerText =preFix + retValue ; " );
            ClienScript.AppendLine(
" } " );
            
return  ClienScript.ToString();
        }


        
void  GetCatalogs()
        {
            catalogs 
=   new  System.Collections.Specialized.ListDictionary();
            catalogs.Add(
" Book " 100 );
            catalogs.Add(
" Drink " 101 );
            catalogs.Add(
" Clothes " 102 );
            catalogs.Add(
" Food " 103 );
        }

        
void  BindDatas()
        {
            listBoxCatalog.DataSource 
=  catalogs;
            listBoxCatalog.DataTextField 
=   " key " ;
            listBoxCatalog.DataBind();
        }


    }
}

你可能感兴趣的:(callback)