|
|
|
 |
 |
Исходник |
 |
|
 |
 |
|
Автор:
|
|
|
Название:
|
Расширенная Одноразовая кнопка |
|
Дата:
|
21 August 2007 |
|
Описание: |
Учитывая интерес, вызванной моим первым контролом "Одноразовая кнопка", на базе уже опубликованных контролов разработал контрол - "Расширенная одноразовая
кнопка"
Контрол реализовывает следующие функции
1. стиль отображения - кнопка, гипер-ссылка, картинка
2. поддержка валидации на клиенте
3. поддержка автосохранения в полях ввода |
| |
Разместить ссылку на этот исходник в форуме вы можете вставив в текст сообщения
следующую строку:
[CODEPOST ID=215]Расширенная Одноразовая кнопка[/CODEPOST] |
| Оценка: |
Проголосовало 3 посетителей, средняя оценка 4.00 |
| Оценить: |
|
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Drawing.Design;
5 using System.Text;
6 using System.Web;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9
10
11 namespace ClickButton
12 {
13 [DefaultProperty("ClickText")]
14 [ToolboxData("<{0}:exOneClickButton runat=server></{0}:exOneClickButton>")]
15 public class exOneClickButton : System.Web.UI.WebControls.Button
16 {
17
18 #region ClickText - Текст, отображаемый на кнопке после нажатия
19
20 private string _clickText;
21
22 /// <summary>
23 /// Текст, отображаемый на кнопке после нажатия
24 /// </summary>
25 [DefaultValue("Идет обработка !")]
26 [Description("Текст, отображаемый на кнопке после нажатия")]
27
28 public string ClickText
29 {
30 get { return _clickText; }
31 set { _clickText = value; }
32 }
33
34 #endregion
35
36 #region IsClicked - Сохраняет факт нажатия на кнопку
37
38 private bool _isClicked;
39
40 /// <summary>
41 /// Сохраняет факт нажатия на кнопку
42 /// </summary>
43 [DefaultValue(false)]
44 [Description("Сохраняет факт нажатия на кнопку ")]
45 [Browsable(false)]
46 private bool IsClicked
47 {
48 get { return _isClicked; }
49 set { _isClicked = value; }
50 }
51
52 #endregion
53
54 #region Src - источник для адреса картинки
55
56 private string _src;
57
58
59 /// <summary>
60 /// Источник для картинки при графическом стиле отображения
61 /// </summary>
62 [Browsable(true), Bindable(true),
63 Description("Источник для картинки при графическом стиле отображения")]
64 [DefaultValue(false)]
65 [NotifyParentProperty(true)]
66 [Category("Control style")]
67 [EditorAttribute(typeof(System.Web.UI.Design.UrlEditor), typeof(UITypeEditor))]
68 public string Src
69 {
70 get { return _src; }
71 set { _src = value; }
72 }
73
74 #endregion
75
76 #region Стиль отображения компоненты
77
78 private ButtonType _buttonStyle;
79
80 /// <summary>
81 /// Стиль отображения компоненты
82 /// </summary>
83 [Browsable(true), Bindable(false)]
84 [Description("Стиль отображения компоненты ")]
85 [DefaultValue(ButtonType.Button)]
86 [NotifyParentProperty(true)]
87 [Category("Control style")]
88 public ButtonType ButtonStyle
89 {
90 get { return _buttonStyle; }
91 set { _buttonStyle = value; }
92 }
93
94 #endregion
95
96
97 #region Перекрытые методы
98
99 protected override void Render(HtmlTextWriter writer)
100 {
101
102
103 switch(ButtonStyle)
104 {
105 case ButtonType.Button:
106 base.Render(writer);
107 break;
108 case ButtonType.Link:
109 writer.AddAttribute(HtmlTextWriterAttribute.Href, "#");
110 if (this.CssClass != "")
111 writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass);
112
113 if (!base.Enabled || IsClicked)
114 writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "true");
115
116 writer.AddAttribute(HtmlTextWriterAttribute.Onclick, this.OnClientClick);
117
118 writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
119 writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
120
121 if (this.ToolTip.Length > 0)
122 writer.AddAttribute(HtmlTextWriterAttribute.Title, this.ToolTip);
123
124 writer.RenderBeginTag(HtmlTextWriterTag.A);
125 writer.Write(this.Text);
126 writer.RenderEndTag( );
127 break;
128 case ButtonType.Image:
129 writer.AddAttribute(HtmlTextWriterAttribute.Type, "Image");
130 writer.AddAttribute(HtmlTextWriterAttribute.Alt, this.Text);
131 writer.AddAttribute(HtmlTextWriterAttribute.Src, GetSrcOnSite());
132
133
134 if (this.CssClass != "")
135 writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass);
136
137 if (!base.Enabled || IsClicked)
138 writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "true");
139
140 writer.AddAttribute(HtmlTextWriterAttribute.Onclick, this.OnClientClick );
141
142
143 writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
144 writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
145
146 if (this.ToolTip.Length > 0)
147 writer.AddAttribute(HtmlTextWriterAttribute.Title, this.ToolTip);
148
149
150 writer.RenderBeginTag(HtmlTextWriterTag.Input);
151
152 writer.RenderEndTag( );
153 break;
154 }
155
156 }
157
158 private string GetSrcOnSite()
159 {
160 if (!this.DesignMode)
161 return (!this.Src.StartsWith("~/")) ?
162 this.Src : this.Src.Replace("~", this.Page.Request.ApplicationPath);
163 else
164 return _src;
165
166
167 }
168
169 /// <param name="savedState">An object that represents the control state to restore.</param>
170 protected override void LoadViewState(object savedState)
171 {
172 if (savedState != null)
173 {
174 object[] state;
175 state = savedState as object[];
176 if (state[0] != null)
177 base.LoadViewState(state[0]);
178 if (state[1] != null)
179 ButtonStyle = (ButtonType)state[1];
180 if (state[2] != null)
181 Src = (string)state[2];
182 if (state[3] != null)
183 IsClicked = (bool)state[3];
184 if (state[4] != null)
185 ClickText = (string)state[4];
186 }
187
188 }
189
190 /// <returns>An object that contains the current view state of the control; otherwise, if there is no view state associated with the control, null.</returns>
191 protected override object SaveViewState()
192 {
193 object[] state;
194 state = new object[5];
195 state[0] = base.SaveViewState( );
196 state[1] = ButtonStyle;
197 state[2] = Src;
198 state[3] = IsClicked;
199 state[4] = ClickText;
200 return state;
201 }
202
203 protected override void OnPreRender(System.EventArgs e)
204 {
205 base.OnPreRender(e);
206
207 if (!IsClicked)
208 {
209
210
211 string strClickedText = string.Empty;
212
213 if (string.Empty.Equals(ClickText))
214 strClickedText = string.Empty;
215 else
216 switch (ButtonStyle)
217 {
218 case ButtonType.Button:
219 strClickedText = " window.event.srcElement.value='" + ClickText + @"';";
220 break;
221 case ButtonType.Link:
222 strClickedText = " window.event.srcElement.innerText='" + ClickText + @"';";
223 break;
224 case ButtonType.Image:
225 strClickedText = " window.event.srcElement.alt='" + ClickText + @"';";
226 break;
227 }
228
229 OnClientClick = "javascript:";
230 if (CausesValidation)
231 {
232 OnClientClick += "if (typeof(Page_ClientValidate) == 'function')"
233 + "{"
234 + " var PageClientValid = Page_ClientValidate();"
235 + " if (!PageClientValid) return false;"
236 + "}";
237 }
238 OnClientClick += "window.event.srcElement.disabled=true;"
239 + strClickedText
240 + Page.ClientScript.GetPostBackEventReference(new PostBackOptions(this))
241 + " ; if (window.external!=null && window.external.AutoCompleteSaveForm!=null) "
242 + " window.external.AutoCompleteSaveForm(document.forms[0].id);"
243 + " return true;";
244
245
246 }
247 else
248 {
249 switch (ButtonStyle)
250 {
251 case ButtonType.Button:
252 Attributes.Add("disabled", "true");
253 break;
254 case ButtonType.Link:
255 case ButtonType.Image:
256 OnClientClick = "javascript:return false;";
257 break;
258 }
259
260 }
261 }
262
263 protected override void OnClick(System.EventArgs e)
264 {
265 base.OnClick(e);
266 IsClicked = true;
267 }
268
269
270 #endregion
271
272
273 public exOneClickButton() : base ()
274 {
275 ButtonStyle = ButtonType.Button;
276 _isClicked = false;
277 _clickText = "Идет обработка !";
278 }
279
280
281 }
282
283
284
285 }
286 |
| Вернуться к списку исходников в категории Создание элементов управления |
|
|
 |
 |
 |
 |
|
|