日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
ASP.NET服務器控件之處理回傳數(shù)據(jù)淺析

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

創(chuàng)新互聯(lián)從2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務公司,擁有項目做網(wǎng)站、網(wǎng)站設計網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元武都做網(wǎng)站,已為上家服務,為武都各地企業(yè)和個人服務,聯(lián)系電話:028-86922220

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

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

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

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

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

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

第二、控件類必須實現(xiàn)IPostBackDataHandler接口,并實現(xiàn)LoadPostData和RaisePostDataChangedEvent方法。LoadPostData方法用來檢查提交給服務器的數(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方法的一個簡單應用。

 
 
 
 
  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;

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

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

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

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

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

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

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

ASP.NET服務器控件2. 典型應用

下面通過一個典型實例說明處理回傳數(shù)據(jù)的核心過程。創(chuàng)建一個自定義文本框控件WebCustomControl,其文本屬性Text因回傳而更改??丶诩虞d回傳數(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. //重寫控件呈現(xiàn)方法RenderContents
  26. protected override void RenderContents(HtmlTextWriter output) { 
  27.  output.AddAttribute(HtmlTextWriterAttribute.Type, "text"); 
  28.  output.AddAttribute(HtmlTextWriterAttribute.Value, Text); 
  29.  output.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID); 
  30.  output.RenderBeginTag(HtmlTextWriterTag.Input); 
  31.  output.RenderEndTag(); 
  32. }
  33. //定義事件對象EventTextChanged 
  34. private static readonly object EventTextChanged = new object();
  35. #region 實現(xiàn)IPostBackDataHandler 成員
  36. bool IPostBackDataHandler.LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection) {
  37.  //比較初始數(shù)據(jù)presentValue和回傳數(shù)據(jù)postedValue 
  38.  string postedValue = postCollection[postDataKey];
  39.  string presentValue = Text; 
  40.  if (presentValue == null || postedValue != presentValue) {
  41. Text = postedValue; 
  42. return true; 
  43.  }
  44.  return false; 
  45. }
  46. void IPostBackDataHandler.RaisePostDataChangedEvent() { 
  47.  OnTextChanged(EventArgs.Empty);
  48. }
  49. #endregion // 實現(xiàn)事件處理程序OnTextChanged 
  50. private void OnTextChanged(EventArgs eventArgs) {
  51.  EventHandler textChangedHandler = (EventHandler)Events[EventTextChanged];
  52.  if (textChangedHandler != null) {
  53. textChangedHandler(this, eventArgs);
  54.  }
  55. }
  56. // 為TextChanged實現(xiàn)事件屬性結(jié)構(gòu) 
  57. public event EventHandler TextChanged {
  58.  add {
  59. Events.AddHandler(EventTextChanged, value);
  60.  }
  61.  remove {
  62. Events.RemoveHandler(EventTextChanged, value);
  63.  }
  64. }
  65.  }
  66. }

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

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

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

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

(4)實現(xiàn)IPostBackDataHandler的方法LoadPostData。比較客戶端發(fā)送的數(shù)據(jù)值與先前服務器提交給客戶端的數(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方法。

下面是應用自定義服務器控件的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服務器控件3、小結(jié)

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


新聞標題:ASP.NET服務器控件之處理回傳數(shù)據(jù)淺析
本文URL:http://www.dlmjj.cn/article/codghip.html