自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ASP.NET服務(wù)器控件之處理回傳數(shù)據(jù)淺析

開發(fā) 后端
ASP.NET服務(wù)器控件之處理回傳數(shù)據(jù)想大家介紹了回傳數(shù)據(jù)的處理方法,為開發(fā)人員解決實現(xiàn)事件方面的實際問題。

ASP.NET服務(wù)器控件之處理回傳數(shù)據(jù)為自定義服務(wù)器控件實現(xiàn)事件,是一個比較復(fù)雜的過程。開發(fā)人員不僅需要根據(jù)上一篇文章中介紹的方法,實現(xiàn)捕獲回傳事件,而且有時候還需要參與回傳數(shù)據(jù)處理過程。本文將通過典型應(yīng)用介紹處理回傳數(shù)據(jù)的方法。

ASP.NET服務(wù)器控件1. 實現(xiàn)處理回傳數(shù)據(jù)

在上一篇文章介紹的捕獲回傳事件過程中,往往都不涉及回傳到服務(wù)器的控件數(shù)據(jù)。開發(fā)人員主要實現(xiàn)IPostBackEventHandler接口就能夠成功捕獲事件,并為之定義事件處理程序。然而,有些服務(wù)器控件在應(yīng)用過程中,涉及回傳數(shù)據(jù)的變化等情況。例如,自定義控件是一個輸入控件,當(dāng)用戶輸入并回傳后,可能會由于回傳數(shù)據(jù)的變化而引發(fā)一些事件。為了處理以上問題,控件類必須實現(xiàn)IPostBackDataHandler接口。下面列舉了接口聲明代碼。

  1. public interface IPostBackDataHandler{   
  2.  
  3. public bool LoadPostData ( string postDataKey, NameValueCollection postCollection );  
  4.  
  5.  public void RaisePostDataChangedEvent ();  
  6.  

IPostBackDataHandler接口用于在創(chuàng)建需要由客戶端回傳到服務(wù)器的窗體數(shù)據(jù)的服務(wù)器控件時使用。如上代碼所示,該接口包括兩個方法,LoadPostData和RaisePostDataChangedEvent。

與實現(xiàn)捕獲回傳事件類似,僅僅在控件類中實現(xiàn)接口是不完整的。下面總結(jié)了為了實現(xiàn)處理回傳數(shù)據(jù)而必須實現(xiàn)的兩個要點。

第一、必須在控件呈現(xiàn)中將控件的name的屬性值設(shè)置為UniqueID。這是由于發(fā)生回傳后,頁框架將在發(fā)送的內(nèi)容中搜索與實現(xiàn)IPostBackDataHandler的服務(wù)器控件的UniqueID匹配的值,然后才能調(diào)用LoadPostData方法。

第二、控件類必須實現(xiàn)IPostBackDataHandler接口,并實現(xiàn)LoadPostData和RaisePostDataChangedEvent方法。LoadPostData方法用來檢查提交給服務(wù)器的數(shù)據(jù)。該方法包含兩個參數(shù):postDataKey表示用于識別控件內(nèi)數(shù)據(jù)的關(guān)鍵值,postData是提交數(shù)據(jù)的集合,其采用Key/Value結(jié)構(gòu)便于使用索引名稱訪問。要訪問集合中的控件數(shù)據(jù),只要采用如下代碼即可:"string nData = postData[postDataKey]; "。在LoadPostData方法中,通過新數(shù)據(jù)(客戶端發(fā)送的數(shù)據(jù)值)與舊數(shù)據(jù)(先前提交給客戶端的數(shù)據(jù)值)進行比較的結(jié)果來確定方法返回值。如果新舊數(shù)據(jù)相同,則說明數(shù)據(jù)沒有被修改,方法返回值為false;如果新舊數(shù)據(jù)不同,則表明舊數(shù)據(jù)已經(jīng)被客戶端修改,方法返回值true。下面是LoadPostData方法的一個簡單應(yīng)用。

  1. public virtual bool LoadPostData(string postDataKey,NameValueCollection postData)  
  2. {  
  3.  string presentValue = Text;  
  4.  //舊數(shù)據(jù)  
  5.  string postedValue = postData[postDataKey];//新數(shù)據(jù)   
  6.  //檢查新舊數(shù)據(jù)  
  7.  if(presentValue.Equals(postedValue) || presentValue == null) {  
  8. Text = postedValue;  
  9. return true;  
  10.  }  
  11.  return false;  
  12. }  

如果LoadPostData方法返回true,.NET框架將自動調(diào)用RaisePostDataChangedEvent方法。該方法用信號要求服務(wù)器控件對象通知ASP.NET應(yīng)用程序該控件的狀態(tài)已更改,控件開發(fā)者可以在該方法中定義根據(jù)數(shù)據(jù)變化引發(fā)的事件。下面是簡單的調(diào)用OnTextChanged方法:

  1. public virtual void RaisePostDataChangedEvent()  
  2. {  
  3.  OnTextChanged(EventArgs.Empty);  
  4. }  

以上是處理回傳數(shù)據(jù)的實現(xiàn)要點,掌握這些要點對于事件處理具有至關(guān)重要的意義。同時,其內(nèi)容也說明了以下.NET框架處理回傳數(shù)據(jù)的過程:

(1)首先在發(fā)送的內(nèi)容中搜索與實現(xiàn)IPostBackDataHandler的服務(wù)器控件的UniqueID匹配的值。

(2)調(diào)用LoadPostData方法,并返回bool值。

(3)如果LoadPostData方法返回true,那么調(diào)用RaisePostDataChangedEvent方法。

(4)執(zhí)行RaisePostDataChangedEvent方法中定義的OnEvent方法。

ASP.NET服務(wù)器控件2. 典型應(yīng)用

下面通過一個典型實例說明處理回傳數(shù)據(jù)的核心過程。創(chuàng)建一個自定義文本框控件WebCustomControl,其文本屬性Text因回傳而更改。控件在加載回傳數(shù)據(jù)后引發(fā)TextChanged事件??丶愒创a如下所示:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Text;  
  5. using System.Web;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. namespace WebControlLibrary{   
  9.  [DefaultProperty("Text")]   
  10.  [ToolboxData("﹤{0}:WebCustomControl runat=server﹥﹤/{0}:WebCustomControl﹥")]   
  11.  public class WebCustomControl : WebControl, IPostBackDataHandler {  
  12. // 實現(xiàn)Text屬性   
  13. [Bindable(true)]  
  14. [Category("Appearance")]   
  15. [DefaultValue("")]  
  16. [Localizable(true)]  
  17. public string Text {   
  18.  get {   
  19. string s = (String)ViewState["Text"];  
  20. return ((s == null) ? String.Empty : s);  
  21.  }  
  22.  set {  
  23. ViewState["Text"] = value;   
  24.  }  
  25. }   
  26. //重寫控件呈現(xiàn)方法RenderContents  
  27. protected override void RenderContents(HtmlTextWriter output) {   
  28.  output.AddAttribute(HtmlTextWriterAttribute.Type, "text");   
  29.  output.AddAttribute(HtmlTextWriterAttribute.Value, Text);   
  30.  output.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);   
  31.  output.RenderBeginTag(HtmlTextWriterTag.Input);   
  32.  output.RenderEndTag();   
  33. }  
  34. //定義事件對象EventTextChanged   
  35. private static readonly object EventTextChanged = new object();  
  36. #region 實現(xiàn)IPostBackDataHandler 成員  
  37. bool IPostBackDataHandler.LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection) {  
  38.  //比較初始數(shù)據(jù)presentValue和回傳數(shù)據(jù)postedValue   
  39.  string postedValue = postCollection[postDataKey];  
  40.  string presentValue = Text;   
  41.  if (presentValue == null || postedValue != presentValue) {  
  42. Text = postedValue;   
  43. return true;   
  44.  }  
  45.  return false;   
  46. }  
  47. void IPostBackDataHandler.RaisePostDataChangedEvent() {   
  48.  OnTextChanged(EventArgs.Empty);  
  49. }  
  50. #endregion // 實現(xiàn)事件處理程序OnTextChanged   
  51. private void OnTextChanged(EventArgs eventArgs) {  
  52.  EventHandler textChangedHandler = (EventHandler)Events[EventTextChanged];  
  53.  if (textChangedHandler != null) {  
  54. textChangedHandler(this, eventArgs);  
  55.  }  
  56. }  
  57. // 為TextChanged實現(xiàn)事件屬性結(jié)構(gòu)   
  58. public event EventHandler TextChanged {  
  59.  add {  
  60. Events.AddHandler(EventTextChanged, value);  
  61.  }  
  62.  remove {  
  63. Events.RemoveHandler(EventTextChanged, value);  
  64.  }  
  65. }  
  66.  }  

以上源代碼實現(xiàn)了一些重要內(nèi)容。

(1)控件類必須實現(xiàn)IPostBackDataHandler,由此可使該控件參與回傳數(shù)據(jù)處理。

(2)定義屬性Text,其屬性值保存在ViewState中。當(dāng)頁面回傳時,包含Text屬性值的ViewState將被提交到服務(wù)器。

(3)重寫RenderContents方法,并在該方法中定義控件呈現(xiàn)邏輯。

(4)實現(xiàn)IPostBackDataHandler的方法LoadPostData。比較客戶端發(fā)送的數(shù)據(jù)值與先前服務(wù)器提交給客戶端的數(shù)據(jù)值是否相同。如果數(shù)據(jù)相同,說明數(shù)據(jù)沒有被修改,那么返回false;如果數(shù)據(jù)不同,則表明數(shù)據(jù)已經(jīng)被客戶端修改,則返回true。

(5)實現(xiàn)IPostBackDataHandler的方法RaisePostDataChangedEvent。如果LoadPostData的返回值為true,則執(zhí)行該方法,即要求調(diào)用OnTextChanged方法。

(6)定義事件屬性結(jié)構(gòu)TextChanged。在Events事件委托列表中,為EventTextChanged事件委托對象定義Add和Remove訪問器。

(7)定義OnTextChanged方法。

下面是應(yīng)用自定義服務(wù)器控件的Default.aspx源代碼:

  1. ﹤%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %﹥  
  2. ﹤%@ Register TagPrefix="wcl" Assembly="WebControlLibrary" Namespace="WebControlLibrary" %﹥  
  3. ﹤!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  4.  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"﹥  
  5. ﹤script runat="server"﹥  
  6.  void demo1_TextChanged(object sender, EventArgs e) {   
  7. label1.Text = "您在文本框中輸入的是 " + demo1.Text;  
  8.  }  
  9. ﹤/script﹥  
  10. ﹤html xmlns="http://www.w3.org/1999/xhtml"﹥  
  11. ﹤head runat="server"﹥   
  12. ﹤title﹥處理回傳數(shù)據(jù)﹤/title﹥  
  13. ﹤/head﹥  
  14. ﹤body﹥  
  15. ﹤form id="form1" runat="server"﹥  
  16. ﹤div﹥  
  17. ﹤wcl:WebCustomControl ID="demo1" runat="server" OnTextChanged="demo1_TextChanged" /﹥  
  18. ﹤asp:Button ID="button1" runat="server" Text="提交" /﹥   
  19. ﹤br /﹥  
  20. ﹤asp:Label ID="label1" runat="server" Font-Size="small"﹥  
  21. ﹤/asp:Label﹥  
  22. ﹤/div﹥  
  23. ﹤/form﹥  
  24. ﹤/body﹥  
  25. ﹤/html﹥ 

在以上代碼中,定義了一個WebCustomControl控件,并為該控件定義TextChanged事件的處理方法demo1_TextChanged。該方法要求修改Label控件的Text屬性值。效果圖如圖1和圖2所示。

頁面初始化效果圖 

圖1 頁面初始化效果圖

頁面提交后的效果圖 

圖2 頁面提交后的效果圖

可能某些讀者會產(chǎn)生誤解,以為上面的實例定義了提交按鈕的Click事件的事件處理方法。實際不然。本實例并沒有為提交按鈕定義Click事件的處理方法,而是通過處理回傳數(shù)據(jù),并定義WebCustomControl控件的TextChanged事件來完成的。

ASP.NET服務(wù)器控件3、小結(jié)

本文針對ASP.NET服務(wù)器控件實現(xiàn)處理回傳數(shù)據(jù)的實現(xiàn)方法進行了介紹。掌握這些內(nèi)容將為開發(fā)出具有高質(zhì)量的服務(wù)器控件打下良好基礎(chǔ)。至此,通過三篇文章的介紹,相信讀者已經(jīng)掌握了為自定義服務(wù)器控件實現(xiàn)事件的基本方法。在隨后的內(nèi)容中,筆者將繼續(xù)介紹利用ASP.NET 技術(shù)創(chuàng)建服務(wù)器控件的其他內(nèi)容。

【編輯推薦】

  1. ASP.NET控件開發(fā)技巧之使用HtmlTextWriter類淺析
  2. ASP.NET控件開發(fā)技巧之ComboBox顯示淺析
  3. ASP.NET控件開發(fā)基礎(chǔ)之自定義控件淺析
  4. ASP.NET服務(wù)器控件Render方法的使用淺析
  5. ASP.NET服務(wù)器控件開發(fā)之實現(xiàn)事件淺析
責(zé)任編輯:仲衡 來源: 天極開發(fā)
相關(guān)推薦

2009-08-06 11:24:50

ASP.NET服務(wù)器控

2009-08-10 14:25:33

ASP.NET服務(wù)器控

2009-08-03 18:00:00

ASP.NET服務(wù)器控

2009-08-05 17:11:51

ASP.NET控件開發(fā)ASP.NET服務(wù)器控

2009-08-07 17:49:44

控件設(shè)計器

2009-07-28 09:46:53

ASP.NET服務(wù)器控

2009-08-06 10:10:31

ASP.NET服務(wù)器控ASP.NET Ren

2009-08-06 10:49:45

ASP.NET服務(wù)器控

2009-08-04 13:10:05

ASP.NET服務(wù)器控

2009-08-10 14:08:15

ASP.NET服務(wù)器控ASP.NET組件設(shè)計

2011-07-12 15:17:02

ASP.net服務(wù)器控件

2009-08-04 17:18:37

2009-08-04 15:20:59

ASP.NET數(shù)據(jù)驗證數(shù)據(jù)驗證控件

2009-08-07 15:34:15

ASP.NET數(shù)據(jù)綁定

2009-07-27 17:25:53

ASP.NET驗證控件

2009-07-30 16:52:38

復(fù)合控件ASP.NET服務(wù)器控

2009-08-06 14:16:04

ASP.NET服務(wù)器控

2009-08-07 15:24:16

ASP.NET模板控件

2009-08-21 17:02:20

ASP.NET異步回調(diào)

2009-08-04 13:39:43

ASP.NET 2.0
點贊
收藏

51CTO技術(shù)棧公眾號