**** Merged r40732-r40872 from MCS ****
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / ImageButton.cs
1 //
2 // System.Web.UI.WebControls.ImageButton.cs
3 //
4 // Authors:
5 //   Gaurav Vaish (gvaish@iitk.ac.in)
6 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) Gaurav Vaish (2002)
9 // (C) 2003 Andreas Nahr
10 //\r
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32 \r
33 using System;\r
34 using System.Collections;\r
35 using System.Collections.Specialized;\r
36 using System.Web;\r
37 using System.Web.UI;\r
38 using System.ComponentModel;\r
39 \r
40 namespace System.Web.UI.WebControls\r
41 {\r
42 #if NET_2_0\r
43         [DesignerAttribute ("System.Web.UI.Design.WebControls.PreviewControlDesigner, System.Design, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.IDesigner")]\r
44 #endif\r
45         [DefaultEvent("Click")]\r
46         public class ImageButton: Image, IPostBackDataHandler, IPostBackEventHandler\r
47 #if NET_2_0\r
48                 , IButtonControl\r
49 #endif\r
50         {\r
51                 private static readonly object ClickEvent   = new object();\r
52                 private static readonly object CommandEvent = new object();\r
53 #if NET_2_0\r
54                 private static readonly object ButtonClickEvent   = new object();\r
55 #endif\r
56 \r
57                 private int x, y;\r
58 \r
59                 public ImageButton(): base()\r
60                 {\r
61                 }\r
62
63 #if NET_2_0\r
64                 [ThemeableAttribute (false)]\r
65 #else\r
66                 [Bindable (false)]\r
67 #endif\r
68                 [DefaultValue (true), WebCategory ("Behavior")]
69                 [WebSysDescription ("Determines if validation is performed when clicked.")]\r
70                 public bool CausesValidation\r
71                 {\r
72                         get\r
73                         {\r
74                                 object o = ViewState["CausesValidation"];\r
75                                 if(o!=null)\r
76                                         return (bool)o;\r
77                                 return true;\r
78                         }\r
79                         set\r
80                         {\r
81                                 ViewState["CausesValidation"] = value;\r
82                         }\r
83                 }\r
84
85 #if NET_2_0\r
86             [ThemeableAttribute (false)]\r
87 #endif\r
88                 [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
89                 [WebSysDescription ("An argument for the Command of this control.")]\r
90                 public string CommandArgument\r
91                 {\r
92                         get\r
93                         {\r
94                                 object o = ViewState["CommandArgument"];\r
95                                 if(o!=null)\r
96                                         return (string)o;\r
97                                 return String.Empty;\r
98                         }\r
99                         set\r
100                         {\r
101                                 ViewState["CommandArgument"] = value;\r
102                         }\r
103                 }\r
104
105 #if NET_2_0\r
106             [ThemeableAttribute (false)]\r
107 #endif\r
108                 [DefaultValue (""), WebCategory ("Behavior")]
109                 [WebSysDescription ("The name of the Command of this control.")]\r
110                 public string CommandName\r
111                 {\r
112                         get\r
113                         {\r
114                                 object o = ViewState["CommandName"];\r
115                                 if(o!=null)\r
116                                         return (string)o;\r
117                                 return String.Empty;\r
118                         }\r
119                         set\r
120                         {\r
121                                 ViewState["CommandName"] = value;\r
122                         }\r
123                 }\r
124 \r
125                 [Browsable (false)]
126                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
127                 protected override HtmlTextWriterTag TagKey\r
128                 {\r
129                         get\r
130                         {\r
131                                 return HtmlTextWriterTag.Input;\r
132                         }\r
133                 }\r
134
135                 [WebCategory ("Action")]
136                 [WebSysDescription ("Raised when the LinkButton is clicked.")]\r
137                 public event ImageClickEventHandler Click\r
138                 {\r
139                         add\r
140                         {\r
141                                 Events.AddHandler(ClickEvent, value);\r
142                         }\r
143                         remove\r
144                         {\r
145                                 Events.RemoveHandler(ClickEvent, value);\r
146                         }\r
147                 }\r
148
149                 [WebCategory ("Action")]
150                 [WebSysDescription ("Raised when a LinkButton Command is executed.")]\r
151                 public event CommandEventHandler Command\r
152                 {\r
153                         add\r
154                         {\r
155                                 Events.AddHandler(CommandEvent, value);\r
156                         }\r
157                         remove\r
158                         {\r
159                                 Events.RemoveHandler(CommandEvent, value);\r
160                         }\r
161                 }\r
162 \r
163 #if NET_2_0\r
164                 [BindableAttribute (true)]\r
165                 [DefaultValueAttribute ("")]\r
166                 public string SoftkeyLabel {\r
167                         get {\r
168                                 string text = (string)ViewState["SoftkeyLabel"];\r
169                                 if (text!=null) return text;\r
170                                 return String.Empty;\r
171                         }\r
172                         set {\r
173                                 ViewState["SoftkeyLabel"] = value;\r
174                         }\r
175                 }\r
176                 \r
177                 [ThemeableAttribute (false)]\r
178                 [EditorAttribute ("System.Web.UI.Design.UrlEditor, System.Design, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]\r
179                 [DefaultValueAttribute ("")]\r
180                 [UrlProperty]\r
181                 public string PostBackUrl {\r
182                         get {\r
183                                 string text = (string)ViewState["PostBackUrl"];\r
184                                 if (text!=null) return text;\r
185                                 return String.Empty;\r
186                         }\r
187                         set {\r
188                                 ViewState["PostBackUrl"] = value;\r
189                         }\r
190                 }\r
191                 \r
192                 [DefaultValueAttribute ("")]\r
193                 [ThemeableAttribute (false)]\r
194                 [WebCategoryAttribute ("Behavior")]\r
195                 public string OnClientClick {\r
196                         get {\r
197                                 string text = (string)ViewState["OnClientClick"];\r
198                                 if (text!=null) return text;\r
199                                 return String.Empty;\r
200                         }\r
201                         set {\r
202                                 ViewState["OnClientClick"] = value;\r
203                         }\r
204                 }\r
205 \r
206                 [DefaultValueAttribute ("")]\r
207                 [ThemeableAttribute (false)]\r
208                 [WebCategoryAttribute ("Behavior")]\r
209                 public string ValidationGroup {\r
210                         get {\r
211                                 string text = (string)ViewState["ValidationGroup"];\r
212                                 if (text!=null) return text;\r
213                                 return String.Empty;\r
214                         }\r
215                         set {\r
216                                 ViewState["ValidationGroup"] = value;\r
217                         }\r
218                 }\r
219                 \r
220                 public string Text {\r
221                         get { return AlternateText; }\r
222                         set { AlternateText = value; }\r
223                 }\r
224                 \r
225                 event EventHandler IButtonControl.Click\r
226                 {\r
227                         add\r
228                         {\r
229                                 Events.AddHandler (ButtonClickEvent, value);\r
230                         }\r
231                         remove\r
232                         {\r
233                                 Events.RemoveHandler (ButtonClickEvent, value);\r
234                         }\r
235                 }\r
236 \r
237                 \r
238                 protected virtual PostBackOptions GetPostBackOptions ()\r
239                 {\r
240                         PostBackOptions ops = new PostBackOptions (this);\r
241                         if (PostBackUrl != "")\r
242                                 ops.ActionUrl = PostBackUrl;\r
243                         ops.PerformValidation = Page.Validators.Count > 0 && CausesValidation;\r
244                         if (ops.PerformValidation && ValidationGroup != "")\r
245                                 ops.ValidationGroup = ValidationGroup;\r
246                         ops.RequiresJavaScriptProtocol = false;\r
247                         return ops;\r
248                 }\r
249 #endif\r
250 \r
251                 protected override void AddAttributesToRender(HtmlTextWriter writer)\r
252                 {\r
253                         writer.AddAttribute(HtmlTextWriterAttribute.Type, "image");\r
254                         writer.AddAttribute(HtmlTextWriterAttribute.Name, UniqueID);\r
255                         \r
256 #if NET_2_0\r
257                         if (Page != null && Enabled) {\r
258                                 string script = "";\r
259                                 \r
260                                 script = OnClientClick;\r
261                                 if (script.Length > 0) script += ";";\r
262                                 \r
263                                 PostBackOptions ops = GetPostBackOptions ();\r
264                                 if (ops.RequiresSpecialPostBack || ops.ClientSubmit) {\r
265                                         script += Page.ClientScript.GetPostBackEventReference (ops);\r
266                                 }\r
267                                 else if (CausesValidation && Page.Validators.Count > 0) {\r
268                                         script += Utils.GetClientValidatedEvent (Page);\r
269                                 }\r
270                                 \r
271                                 if (script != "") {\r
272                                         writer.AddAttribute (HtmlTextWriterAttribute.Onclick, script);\r
273                                         writer.AddAttribute ("language", "javascript");\r
274                                 }\r
275                         }\r
276                         \r
277                         if (!Enabled)\r
278                                 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");\r
279 #else\r
280                         if (Page != null && CausesValidation && Page.Validators.Count > 0) {\r
281                                 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, Utils.GetClientValidatedEvent (Page));\r
282                                 writer.AddAttribute ("language", "javascript");\r
283                         }\r
284 #endif\r
285                         base.AddAttributesToRender(writer);\r
286                 }\r
287 \r
288                 protected virtual void OnClick(ImageClickEventArgs e)\r
289                 {\r
290                         if(Events != null)\r
291                         {\r
292                                 ImageClickEventHandler iceh = (ImageClickEventHandler)(Events[ClickEvent]);\r
293                                 if(iceh != null)\r
294                                         iceh(this, e);\r
295 \r
296 #if NET_2_0\r
297                                 EventHandler eh = (EventHandler) (Events [ButtonClickEvent]);\r
298                                 if(eh != null)\r
299                                         eh (this, e);\r
300 #endif\r
301                         }\r
302                 }\r
303 \r
304                 protected virtual void OnCommand(CommandEventArgs e)\r
305                 {\r
306                         if(Events != null)\r
307                         {\r
308                                 CommandEventHandler ceh = (CommandEventHandler)(Events[CommandEvent]);\r
309                                 if(ceh != null)\r
310                                         ceh(this, e);\r
311                                 RaiseBubbleEvent(this, e);\r
312                         }\r
313                 }\r
314 \r
315                 protected override void OnPreRender(EventArgs e)\r
316                 {\r
317                         if(Page != null)\r
318                         {\r
319                                 Page.RegisterRequiresPostBack(this);\r
320                         }\r
321                 }\r
322 \r
323 #if NET_2_0\r
324                 bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)\r
325                 {\r
326                         return LoadPostData (postDataKey, postCollection);\r
327                 }\r
328                 \r
329                 protected virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)\r
330 #else\r
331                 bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)\r
332 #endif\r
333                 {\r
334                         string xCoord = postCollection[UniqueID + ".x"];\r
335                         string yCoord = postCollection[UniqueID + ".y"];\r
336                         string id = postCollection[UniqueID];
337                         if(xCoord != null && yCoord != null && xCoord.Length > 0 && yCoord.Length > 0)\r
338                         {\r
339                                 x = Int32.Parse(xCoord);\r
340                                 y = Int32.Parse(yCoord);\r
341                                 Page.RegisterRequiresRaiseEvent(this);
342                         } else if (id != null)
343                         {
344                                 //
345                                 // This is a workaround for bug #49819. It appears that the .x and .y
346                                 // values are not being posted, and only the x value is being posted
347                                 // with the ctrl's id as the key.
348                                 //
349                                 x = Int32.Parse (id);
350                                 Page.RegisterRequiresRaiseEvent (this);
351                         }
352                         return false;\r
353                 }\r
354 \r
355 #if NET_2_0\r
356                 void IPostBackDataHandler.RaisePostDataChangedEvent()\r
357                 {\r
358                         RaisePostDataChangedEvent ();\r
359                 }\r
360                 \r
361                 protected virtual void RaisePostDataChangedEvent ()\r
362                 {\r
363                 }\r
364 #else\r
365                 void IPostBackDataHandler.RaisePostDataChangedEvent()\r
366                 {\r
367                 }\r
368 #endif\r
369 \r
370 \r
371 #if NET_2_0\r
372                 void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)\r
373                 {\r
374                         RaisePostBackEvent (eventArgument);\r
375                 }\r
376                 \r
377                 protected virtual void RaisePostBackEvent(string eventArgument)\r
378                 {\r
379                         if(CausesValidation)\r
380                                 Page.Validate (ValidationGroup);\r
381 \r
382                         OnClick(new ImageClickEventArgs(x, y));\r
383                         OnCommand(new CommandEventArgs(CommandName, CommandArgument));\r
384                 }\r
385 #else\r
386                 void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)\r
387                 {\r
388                         if(CausesValidation)\r
389                                 Page.Validate ();\r
390 \r
391                         OnClick(new ImageClickEventArgs(x, y));\r
392                         OnCommand(new CommandEventArgs(CommandName, CommandArgument));\r
393                 }\r
394 #endif\r
395         }\r
396 }\r