2004-06-09 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[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 \r
12 using System;\r
13 using System.Collections;\r
14 using System.Collections.Specialized;\r
15 using System.Web;\r
16 using System.Web.UI;\r
17 using System.ComponentModel;\r
18 \r
19 namespace System.Web.UI.WebControls\r
20 {\r
21         [DefaultEvent("Click")]\r
22         public class ImageButton: Image, IPostBackDataHandler, IPostBackEventHandler\r
23         {\r
24                 private static readonly object ClickEvent   = new object();\r
25                 private static readonly object CommandEvent = new object();\r
26 \r
27                 private int x, y;\r
28 \r
29                 public ImageButton(): base()\r
30                 {\r
31                 }\r
32
33                 [DefaultValue (true), Bindable (false), WebCategory ("Behavior")]
34                 [WebSysDescription ("Determines if validation is performed when clicked.")]\r
35                 public bool CausesValidation\r
36                 {\r
37                         get\r
38                         {\r
39                                 object o = ViewState["CausesValidation"];\r
40                                 if(o!=null)\r
41                                         return (bool)o;\r
42                                 return true;\r
43                         }\r
44                         set\r
45                         {\r
46                                 ViewState["CausesValidation"] = value;\r
47                         }\r
48                 }\r
49
50                 [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
51                 [WebSysDescription ("An argument for the Command of this control.")]\r
52                 public string CommandArgument\r
53                 {\r
54                         get\r
55                         {\r
56                                 object o = ViewState["CommandArgument"];\r
57                                 if(o!=null)\r
58                                         return (string)o;\r
59                                 return String.Empty;\r
60                         }\r
61                         set\r
62                         {\r
63                                 ViewState["CommandArgument"] = value;\r
64                         }\r
65                 }\r
66
67                 [DefaultValue (""), WebCategory ("Behavior")]
68                 [WebSysDescription ("The name of the Command of this control.")]\r
69                 public string CommandName\r
70                 {\r
71                         get\r
72                         {\r
73                                 object o = ViewState["CommandName"];\r
74                                 if(o!=null)\r
75                                         return (string)o;\r
76                                 return String.Empty;\r
77                         }\r
78                         set\r
79                         {\r
80                                 ViewState["CommandName"] = value;\r
81                         }\r
82                 }\r
83 \r
84                 [Browsable (false)]
85                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
86                 protected override HtmlTextWriterTag TagKey\r
87                 {\r
88                         get\r
89                         {\r
90                                 return HtmlTextWriterTag.Input;\r
91                         }\r
92                 }\r
93
94                 [WebCategory ("Action")]
95                 [WebSysDescription ("Raised when the LinkButton is clicked.")]\r
96                 public event ImageClickEventHandler Click\r
97                 {\r
98                         add\r
99                         {\r
100                                 Events.AddHandler(ClickEvent, value);\r
101                         }\r
102                         remove\r
103                         {\r
104                                 Events.RemoveHandler(ClickEvent, value);\r
105                         }\r
106                 }\r
107
108                 [WebCategory ("Action")]
109                 [WebSysDescription ("Raised when a LinkButton Command is executed.")]\r
110                 public event CommandEventHandler Command\r
111                 {\r
112                         add\r
113                         {\r
114                                 Events.AddHandler(CommandEvent, value);\r
115                         }\r
116                         remove\r
117                         {\r
118                                 Events.RemoveHandler(CommandEvent, value);\r
119                         }\r
120                 }\r
121 \r
122                 protected override void AddAttributesToRender(HtmlTextWriter writer)\r
123                 {\r
124                         writer.AddAttribute(HtmlTextWriterAttribute.Type, "image");\r
125                         writer.AddAttribute(HtmlTextWriterAttribute.Name, UniqueID);\r
126                         if(Page != null && CausesValidation)\r
127                         {\r
128                                 if(Page.Validators.Count > 0)\r
129                                 {\r
130                                         writer.AddAttribute(HtmlTextWriterAttribute.Onclick, Utils.GetClientValidatedEvent(Page));\r
131                                         writer.AddAttribute("language", "javascript");\r
132                                 }\r
133                         }\r
134                         base.AddAttributesToRender(writer);\r
135                 }\r
136 \r
137                 protected virtual void OnClick(ImageClickEventArgs e)\r
138                 {\r
139                         if(Events != null)\r
140                         {\r
141                                 ImageClickEventHandler iceh = (ImageClickEventHandler)(Events[ClickEvent]);\r
142                                 if(iceh != null)\r
143                                         iceh(this, e);\r
144                         }\r
145                 }\r
146 \r
147                 protected virtual void OnCommand(CommandEventArgs e)\r
148                 {\r
149                         if(Events != null)\r
150                         {\r
151                                 CommandEventHandler ceh = (CommandEventHandler)(Events[CommandEvent]);\r
152                                 if(ceh != null)\r
153                                         ceh(this, e);\r
154                                 RaiseBubbleEvent(this, e);\r
155                         }\r
156                 }\r
157 \r
158                 protected override void OnPreRender(EventArgs e)\r
159                 {\r
160                         if(Page != null)\r
161                         {\r
162                                 Page.RegisterRequiresPostBack(this);\r
163                         }\r
164                 }\r
165 \r
166                 bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)\r
167                 {\r
168                         string xCoord = postCollection[UniqueID + ".x"];\r
169                         string yCoord = postCollection[UniqueID + ".y"];\r
170                         string id = postCollection[UniqueID];
171                         if(xCoord != null && yCoord != null && xCoord.Length > 0 && yCoord.Length > 0)\r
172                         {\r
173                                 x = Int32.Parse(xCoord);\r
174                                 y = Int32.Parse(yCoord);\r
175                                 Page.RegisterRequiresRaiseEvent(this);
176                         } else if (id != null)
177                         {
178                                 //
179                                 // This is a workaround for bug #49819. It appears that the .x and .y
180                                 // values are not being posted, and only the x value is being posted
181                                 // with the ctrl's id as the key.
182                                 //
183                                 x = Int32.Parse (id);
184                                 Page.RegisterRequiresRaiseEvent (this);
185                         }
186                         return false;\r
187                 }\r
188 \r
189                 void IPostBackDataHandler.RaisePostDataChangedEvent()\r
190                 {\r
191                 }\r
192 \r
193                 void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)\r
194                 {\r
195                         if(CausesValidation)\r
196                                 Page.Validate();\r
197                         OnClick(new ImageClickEventArgs(x, y));\r
198                         OnCommand(new CommandEventArgs(CommandName, CommandArgument));\r
199                 }\r
200         }\r
201 }\r