2 // System.Web.UI.WebControls.ImageButton.cs
5 // Gaurav Vaish (gvaish@iitk.ac.in)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 // (C) Gaurav Vaish (2002)
9 // (C) 2003 Andreas Nahr
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
34 using System.Collections;
\r
35 using System.Collections.Specialized;
\r
37 using System.Web.UI;
\r
38 using System.ComponentModel;
\r
40 namespace System.Web.UI.WebControls
\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
45 [DefaultEvent("Click")]
\r
46 public class ImageButton: Image, IPostBackDataHandler, IPostBackEventHandler
\r
51 private static readonly object ClickEvent = new object();
\r
52 private static readonly object CommandEvent = new object();
\r
54 private static readonly object ButtonClickEvent = new object();
\r
59 public ImageButton(): base()
\r
64 [ThemeableAttribute (false)]
\r
68 [DefaultValue (true), WebCategory ("Behavior")]
69 [WebSysDescription ("Determines if validation is performed when clicked.")]
\r
70 public bool CausesValidation
\r
74 object o = ViewState["CausesValidation"];
\r
81 ViewState["CausesValidation"] = value;
\r
86 [ThemeableAttribute (false)]
\r
88 [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
89 [WebSysDescription ("An argument for the Command of this control.")]
\r
90 public string CommandArgument
\r
94 object o = ViewState["CommandArgument"];
\r
97 return String.Empty;
\r
101 ViewState["CommandArgument"] = value;
\r
106 [ThemeableAttribute (false)]
\r
108 [DefaultValue (""), WebCategory ("Behavior")]
109 [WebSysDescription ("The name of the Command of this control.")]
\r
110 public string CommandName
\r
114 object o = ViewState["CommandName"];
\r
117 return String.Empty;
\r
121 ViewState["CommandName"] = value;
\r
126 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
127 protected override HtmlTextWriterTag TagKey
\r
131 return HtmlTextWriterTag.Input;
\r
135 [WebCategory ("Action")]
136 [WebSysDescription ("Raised when the LinkButton is clicked.")]
\r
137 public event ImageClickEventHandler Click
\r
141 Events.AddHandler(ClickEvent, value);
\r
145 Events.RemoveHandler(ClickEvent, value);
\r
149 [WebCategory ("Action")]
150 [WebSysDescription ("Raised when a LinkButton Command is executed.")]
\r
151 public event CommandEventHandler Command
\r
155 Events.AddHandler(CommandEvent, value);
\r
159 Events.RemoveHandler(CommandEvent, value);
\r
164 [BindableAttribute (true)]
\r
165 [DefaultValueAttribute ("")]
\r
166 public string SoftkeyLabel {
\r
168 string text = (string)ViewState["SoftkeyLabel"];
\r
169 if (text!=null) return text;
\r
170 return String.Empty;
\r
173 ViewState["SoftkeyLabel"] = value;
\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
181 public string PostBackUrl {
\r
183 string text = (string)ViewState["PostBackUrl"];
\r
184 if (text!=null) return text;
\r
185 return String.Empty;
\r
188 ViewState["PostBackUrl"] = value;
\r
192 [DefaultValueAttribute ("")]
\r
193 [ThemeableAttribute (false)]
\r
194 [WebCategoryAttribute ("Behavior")]
\r
195 public string OnClientClick {
\r
197 string text = (string)ViewState["OnClientClick"];
\r
198 if (text!=null) return text;
\r
199 return String.Empty;
\r
202 ViewState["OnClientClick"] = value;
\r
206 [DefaultValueAttribute ("")]
\r
207 [ThemeableAttribute (false)]
\r
208 [WebCategoryAttribute ("Behavior")]
\r
209 public string ValidationGroup {
\r
211 string text = (string)ViewState["ValidationGroup"];
\r
212 if (text!=null) return text;
\r
213 return String.Empty;
\r
216 ViewState["ValidationGroup"] = value;
\r
220 public string Text {
\r
221 get { return AlternateText; }
\r
222 set { AlternateText = value; }
\r
225 event EventHandler IButtonControl.Click
\r
229 Events.AddHandler (ButtonClickEvent, value);
\r
233 Events.RemoveHandler (ButtonClickEvent, value);
\r
238 protected virtual PostBackOptions GetPostBackOptions ()
\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
251 protected override void AddAttributesToRender(HtmlTextWriter writer)
\r
253 writer.AddAttribute(HtmlTextWriterAttribute.Type, "image");
\r
254 writer.AddAttribute(HtmlTextWriterAttribute.Name, UniqueID);
\r
257 if (Page != null && Enabled) {
\r
258 string script = "";
\r
260 script = OnClientClick;
\r
261 if (script.Length > 0) script += ";";
\r
263 PostBackOptions ops = GetPostBackOptions ();
\r
264 if (ops.RequiresSpecialPostBack || ops.ClientSubmit) {
\r
265 script += Page.ClientScript.GetPostBackEventReference (ops);
\r
267 else if (CausesValidation && Page.Validators.Count > 0) {
\r
268 script += Utils.GetClientValidatedEvent (Page);
\r
271 if (script != "") {
\r
272 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, script);
\r
273 writer.AddAttribute ("language", "javascript");
\r
278 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
\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
285 base.AddAttributesToRender(writer);
\r
288 protected virtual void OnClick(ImageClickEventArgs e)
\r
292 ImageClickEventHandler iceh = (ImageClickEventHandler)(Events[ClickEvent]);
\r
297 EventHandler eh = (EventHandler) (Events [ButtonClickEvent]);
\r
304 protected virtual void OnCommand(CommandEventArgs e)
\r
308 CommandEventHandler ceh = (CommandEventHandler)(Events[CommandEvent]);
\r
311 RaiseBubbleEvent(this, e);
\r
315 protected override void OnPreRender(EventArgs e)
\r
319 Page.RegisterRequiresPostBack(this);
\r
324 bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
\r
326 return LoadPostData (postDataKey, postCollection);
\r
329 protected virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
\r
331 bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
\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
339 x = Int32.Parse(xCoord);
\r
340 y = Int32.Parse(yCoord);
\r
341 Page.RegisterRequiresRaiseEvent(this);
342 } else if (id != null)
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.
349 x = Int32.Parse (id);
350 Page.RegisterRequiresRaiseEvent (this);
356 void IPostBackDataHandler.RaisePostDataChangedEvent()
\r
358 RaisePostDataChangedEvent ();
\r
361 protected virtual void RaisePostDataChangedEvent ()
\r
365 void IPostBackDataHandler.RaisePostDataChangedEvent()
\r
372 void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
\r
374 RaisePostBackEvent (eventArgument);
\r
377 protected virtual void RaisePostBackEvent(string eventArgument)
\r
379 if(CausesValidation)
\r
380 Page.Validate (ValidationGroup);
\r
382 OnClick(new ImageClickEventArgs(x, y));
\r
383 OnCommand(new CommandEventArgs(CommandName, CommandArgument));
\r
386 void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
\r
388 if(CausesValidation)
\r
391 OnClick(new ImageClickEventArgs(x, y));
\r
392 OnCommand(new CommandEventArgs(CommandName, CommandArgument));
\r