一个给定的类(或结构)只能定义一个static构造器,无论生成多少个实例,一个类的static构造器只执行一次,不能为static构造器指定访问修饰符,不能带有任何参数
完整的控件代码
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 cn
blogs.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