ASP.NET2.0中控件的簡單異步回調_.Net教程

      編輯Tag賺U幣
      教程Tag:暫無Tag,歡迎添加,賺取U幣!

      推薦:asp.net客戶端回調功能的實現機制
      實現的過程基本上就是:讓要實現客戶端回調功能的頁面或者空間實現System.Web.UI.ICallbackEventHandler的接口,即2個方法:void RaiseCallbackEvent(string eventArgument),這個是當客戶端觸

      雖然已經有了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");
      }


      分享:ASP.Net用MD5和SHA1加密的幾種方法
      首先簡單介紹一下MD5和SHA1: MD5的全稱是Message-Digest Algorithm 5(信息-摘要算法),在90年代初由Mit Laboratory for Computer Science和Rsa data security inc的Ronald l. rivest開發出

      來源:模板無憂//所屬分類:.Net教程/更新時間:2008-08-22
      相關.Net教程