ASP.NET 2.0 中控件的简单异步回调

     虽然已经有了ASP.NET AJAX了,最近学习ASP.NET控件的时候,逐步理解了原始的控件异步回调(代码取自《ASP.NET 2.0 高级编程》):
     首先,在Render事件中添加好一个事件
     
None.gif   protected   override   void  RenderContents(HtmlTextWriter output)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            output.RenderBeginTag(HtmlTextWriterTag.Div);
InBlock.gif
InBlock.gif            output.AddAttribute(HtmlTextWriterAttribute.Type, 
"text");
InBlock.gif            output.AddAttribute(HtmlTextWriterAttribute.Id, 
this.ClientID);
InBlock.gif            output.AddAttribute(HtmlTextWriterAttribute.Name, 
this.ClientID);
InBlock.gif            output.AddAttribute(HtmlTextWriterAttribute.Value, 
this.Text);
InBlock.gif
InBlock.gif            output.AddAttribute(
"OnBlur""ClientCallback();");
InBlock.gif            
this.AddAttributesToRender(output);
InBlock.gif            output.RenderBeginTag(HtmlTextWriterTag.Input);
InBlock.gif            output.RenderEndTag();
InBlock.gif            output.RenderEndTag();
ExpandedBlockEnd.gif        }

    这里最重要的就是output.AddAttribute("OnBlur","ClientCallback();");
    然后在OnPreRender事件中,添加如下代码:
None.gif protected   override   void  OnPreRender(EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
//Page.ClientScript.RegisterClientScriptInclude("UtilityFunctions", "JScript.js");
InBlock.gif
            Page.ClientScript.RegisterStartupScript(typeof(Page), "ControlFocus""document.getElementById('" + this.ClientID + "').focus();"true);
InBlock.gif            Page.ClientScript.RegisterStartupScript(
typeof(Page),"ClientCallback","function ClientCallback() {"+"args=document.getElementById('"+this.ClientID+"').value;"+Page.ClientScript.GetCallbackEventReference(this,"args","CallbackHandler",null,"ErrorHandler",true)+"}");
InBlock.gif            
//向服务器发送请求,由服务器端生成回调的客户端脚本。
ExpandedBlockEnd.gif
        }

None.gif
      也就是在服务器端生成客户端代码,注意最后一个方法GetCallbackEventReference,我理解的是在服务器端捕捉了客户端的请求之后,生成相应的客户端脚本,在服务器端回调的时候,客户端决定用什么函数处理回调和错误。
      服务器端实现接口的一个方法,也就是接收到客户端的请求之后,由服务器端先处理,然后再把结果和相应代码发回客户端。
    
  #region  ICallbackEventHandler Members
        
public   string  RaiseCallbackEvent( string  eventArgument)
        {
            
int  result;
            
if  ( ! Int32.TryParse(eventArgument,  out  result))
                
throw   new  Exception( " The method is not implemented. " );
            
return   " Valid Data " ;
        }
        
#endregion
    最后,在jscript.js文件中写好相应的回调处理函数即可:
var  args;
var  ctx;


function  ValidateText(ctl)
{
   
if (ctl.value == '')
   {
      alert(
" Please enter a value " );
      ctl.focus();
   }
}

function  CallbackHandler(args,ctx)
{
    alert(
" The data is valid " );
}

function  ErrorHandler(args,ctx)
{
    alert(
" The data is not a number " );
}

转载于:https://www.cnblogs.com/tyrael007/archive/2007/07/20/824728.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值