• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • ASP.NET2.0中控件的簡單異步回調

    發表于:2007-09-07來源:作者:點擊數: 標簽:
    雖然已經有了ASP.NET AJAX了,最近學習ASP.NET控件的時候,逐步理解了原始的控件異步回調(代碼取自《ASP.NET 2.0 高級編程》): 首先,在Render事件中添加好一個事件 protected override void RenderContents(HtmlTextWriter output) { output.RenderBegin
    雖然已經有了ASP.NET AJAX了,最近學習ASP.NET控件的時候,逐步理解了原始的控件異步回調(代碼取自《ASP.NET 2.0 高級編程》):
    首先,在Render事件中添加好一個事件
    protected override void RenderContents(HtmlTextWriter output)
    {
    output.RenderBeginTag(HtmlTextWriterTag.Div);

    output.AddAttribute(HtmlTextWriterAttribute.Type, "text");
    output.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
    output.AddAttribute(HtmlTextWriterAttribute.Name, this.ClientID);
    output.AddAttribute(HtmlTextWriterAttribute.Value, this.Text);

    output.AddAttribute("OnBlur", "ClientCallback();");
    this.AddAttributesToRender(output);
    output.RenderBeginTag(HtmlTextWriterTag.Input);
    output.RenderEndTag();
    output.RenderEndTag();
    }
    這里最重要的就是output.AddAttribute("OnBlur","ClientCallback();");

    然后在OnPreRender事件中,添加如下代碼: protected override void OnPreRe
    nder(EventArgs e)
    {
    //Page.ClientScript.RegisterClientScriptInclude("UtilityFunctions", "JScript.js");
    Page.ClientScript.RegisterStartupScript(typeof(Page), "ControlFocus", "document.getElementById('" + this.ClientID + "').focus();", true);
    Page.ClientScript.RegisterStartupScript(typeof(Page),"ClientCallback","function ClientCallback() {"+"args=document.getElementById('"+this.ClientID+"').value;"+Page.ClientScript.GetCallbackEventReference(this,"args","CallbackHandler",null,"ErrorHandler",true)+"}");
    //向服務器發送請求,由服務器端生成回調的客戶端腳本。
    }
    也就是在服務器端生成客戶端代碼,注意最后一個方法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");
    }
    熱門推薦 自動取款機的Windows出現搞笑錯 Win Vista系統八種高級應用技巧


    【責任編輯 彭凡】

    原文轉自:http://www.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>