精彩专题推荐:建站之入门课 建站之必修课 建站之关键课 网站价值所在 流量提高专题 css+div 标准 个人网站打造全过程
返回建站学首页
导航:
建站首页 | 网站设计 | 网站开发 | 网站运营 | 网页软件 | 建站指南 | 搜索优化 | 图像处理 | 视频教程 | 书籍教程 | 建站专题
当前位置:首页>网站开发>asp.net教程>正文

.Net组件开发介绍 开发ajax控件


来源:博客圆 时间:07-10-15 点击: 点击这里收藏本文

  3.6 实现ICallbackEventHandler接口

  接口的两个方法。

 

public void RaiseCallbackEvent(string eventArgument)
  {
  TextChangedEventArgs args = new TextChangedEventArgs(eventArgument);
  OnTextChanged(this, args);
  }
  public string GetCallbackResult()
  {
  return ReturnString;
  }

 

  3.7静态构造器

 

  static ajaxText()
   {
   eventTextChanged = new object();
   }

 

  一个给定的类(或结构)只能定义一个static构造器,无论生成多少个实例,一个类的static构造器只执行一次,不能为static构造器指定访问修饰符,不能带有任何参数

  static构造器在程序员创建第一个实例的时候,在头一次访问static成员之前被调用,static构造器在所有实例级别的构造器之前执行。

  4。控件代码:

 

 完整的控件代码
  1using System;
  2using System.Data;
  3using System.Configuration;
  4using System.Web;
  5using System.Web.Security;
  6using System.Web.UI;
  7using System.Web.UI.WebControls;
  8using System.Web.UI.WebControls.WebParts;
  9using System.Web.UI.HtmlControls;
  10using System.ComponentModel;
  11using System.Collections.Specialized;
  12/**//// 
  13/// ajaxText 的摘要说明
  14/// 
  15///
  16namespace cnblogs.suiqirui
  17{
  18 public class TextChangedEventArgs : EventArgs
  19 {
  20
  21
  22 public TextChangedEventArgs()
  23 {
  24 Text = "";
  25
  26 }
  27
  28 public TextChangedEventArgs(string _Text)
  29 {
  30 Text = _Text;
  31
  32 }
  33 private string Text = "";
  34 public string TextValue
  35 {
  36 get { return Text; }
  37 }
  38 }
  39 [DefaultEvent("TextChanged")]
  40 [ToolboxData("<{0}:ajaxtext runat=server>")]
  41 public class ajaxText : WebControl, ICallbackEventHandler
  42 {
  43
  44 private string returnstring;
  45 public delegate void TextChangedEventHandler(object sender, TextChangedEventArgs e);//申明一个委托。
  46 private static readonly object eventTextChanged;
  47
  48 public event TextChangedEventHandler TextChanged
  49 {
  50 add
  51 {
  52
  53 Events.AddHandler(eventTextChanged, value);
  54 }
  55 remove
  56 {
  57
  58 Events.RemoveHandler(eventTextChanged, value);
  59 }
  60 }
  61
  62 static ajaxText()
  63 {
  64 eventTextChanged = new object();
  65 }
  66 属性列表#region 属性列表
  67 [Description("得到或者设置一个text值")]
  68
  69 public string Text
  70 {
  71 get
  72 {
  73
  74 object o = ViewState["Text"];
  75 return o == null ? "" : (string)o;
  76 }
  77 set
  78 {
  79
  80 ViewState["Text"] = value;
  81 }
  82
  83
  84 }
  85
  86 private string ReturnString
  87 {
  88 get
  89 {
  90 return (string)ViewState["ReturnString"];
  91
  92 }
  93 set
  94 {
  95
  96 ViewState["ReturnString"] = value;
  97 }
  98
  99
  100 }
  101 public bool IsValid
  102 {
  103
  104 get
  105 {
  106
  107 object o = ViewState["IsValid"];
  108 return o == null ? false : (bool)o;
  109 }
  110 set
  111 {
  112
  113 ViewState["IsValid"] = value;
  114
  115 }
  116 }
  117
  118 [Description("设置或获取客户端脚本名字")]
  119 public string ClientCallBackScript
  120 {
  121
  122 get
  123 {
  124
  125 object o = ViewState["ClientCallBackScript"];
  126 return o == null ? "null" : o.ToString();
  127 }
  128 set
  129 {
  130
  131 ViewState["ClientCallBackScript"] = value;
  132 }
  133 }
  134
  135
  136 #endregion
  137
  138
  139
  140 public string GetCallbackResult()
  141 {
  142
  143 return ReturnString;
  144 }
  145 protected override void Render(HtmlTextWriter writer)
  146 {
  147 if (base.Page == null)
  148 {
  149 base.Page.VerifyRenderingInServerForm(this);
  150 }
  151 string callbackScript = Page.ClientScript.GetCallbackEventReference(this, "this.value", ClientCallBackScript, null);
  152
  153 // writer.WriteBeginTag("input");
  154
  155 writer.AddAttribute("onblur", callbackScript);
  156 writer.Write(" 
  157 writer.Write("\" value=\"" + this.Text + "\" />");
  158 base.Render(writer);
  159
  160
  161 }
  162
  163 public void RaiseCallbackEvent(string eventArgument)
  164 {
  165
  166
  167 TextChangedEventArgs args = new TextChangedEventArgs(eventArgument);
  168
  169
  170 OnTextChanged(this, args);
  171 ReturnString = Convert.ToString(IsValid);
  172
  173
  174
  175
  176 }
  177
  178 protected virtual void OnTextChanged(object sender, TextChangedEventArgs e)
  179 {
  180 TextChangedEventHandler handler = Events[eventTextChanged] as TextChangedEventHandler;
  181 if (handler != null)
  182 {
  183 Text = e.TextValue;
  184 handler(this, e);
  185
  186 }
  187 }
  188 public ajaxText()
  189 {
  190 //
  191 // TODO: 在此处添加构造函数逻辑
  192 //
  193 }
  194 }
  195}
  196
  197

9 7 3 1 2 3 4 5 4 8 :

  把此文章收藏到:          
广而告之
文章搜索
  • Google JZxue.Com

关于我们 | 联系我们 | 友情链接 | 网站地图
Copyright © 2005 - 2006 建站学 All rights reserved.