2 // System.Web.UI.WebControls.ImageButton.cs
5 // Jordi Mas i Hernandez (jordi@ximian.com)
7 // (C) 2005-2010 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections.Specialized;
32 using System.ComponentModel;
33 using System.Security.Permissions;
35 namespace System.Web.UI.WebControls
38 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41 [DefaultEvent("Click")]
42 [Designer ("System.Web.UI.Design.WebControls.PreviewControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
43 [SupportsEventValidation]
44 public class ImageButton : Image, IPostBackDataHandler, IPostBackEventHandler, IButtonControl
46 static readonly object ClickEvent = new object ();
47 static readonly object CommandEvent = new object ();
56 [WebSysDescription ("")]
57 [WebCategory ("Behavior")]
59 public virtual bool CausesValidation {
60 get { return ViewState.GetBool ("CausesValidation", true); }
61 set { ViewState ["CausesValidation"] = value; }
66 [WebSysDescription ("")]
67 [WebCategory ("Behavior")]
69 public string CommandArgument {
70 get { return ViewState.GetString ("CommandArgument", String.Empty); }
71 set { ViewState ["CommandArgument"] = value; }
75 [WebSysDescription ("")]
76 [WebCategory ("Behavior")]
78 public string CommandName {
79 get { return ViewState.GetString ("CommandName", String.Empty); }
80 set { ViewState ["CommandName"] = value; }
83 [EditorBrowsable (EditorBrowsableState.Always)]
87 public virtual new bool Enabled {
88 // Should there be any special code below? Doesn't look so...
89 get { return base.Enabled; }
90 set { base.Enabled = value; }
94 [EditorBrowsable (EditorBrowsableState.Never)]
96 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
97 public override bool GenerateEmptyAlternateText {
99 set { throw new NotSupportedException (); }
104 public virtual string OnClientClick {
105 get { return ViewState.GetString ("OnClientClick", String.Empty); }
106 set { ViewState ["OnClientClick"] = value; }
110 [UrlProperty ("*.aspx")]
112 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
113 public virtual string PostBackUrl {
114 get { return ViewState.GetString ("PostBackUrl", String.Empty); }
115 set { ViewState["PostBackUrl"] = value; }
120 [WebSysDescription ("")]
121 [WebCategory ("Behavior")]
122 public virtual string ValidationGroup {
123 get { return ViewState.GetString ("ValidationGroup", String.Empty); }
124 set { ViewState ["ValidationGroup"] = value; }
128 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
129 protected override HtmlTextWriterTag TagKey {
130 get { return HtmlTextWriterTag.Input; }
133 // Gets or sets the value of the ImageButton control's AlternateText property. (MSDN)
134 protected virtual string Text {
135 get { return AlternateText; }
136 set { AlternateText = value; }
138 public override bool SupportsDisabledAttribute {
139 get { return RenderingCompatibilityLessThan40; }
141 protected override void AddAttributesToRender (HtmlTextWriter writer)
145 page.VerifyRenderingInServerForm (this);
147 writer.AddAttribute (HtmlTextWriterAttribute.Type, "image", false);
148 writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
150 base.AddAttributesToRender (writer);
152 string onclick = OnClientClick;
153 if (!String.IsNullOrEmpty (onclick))
154 onclick = ClientScriptManager.EnsureEndsWithSemicolon (onclick);
156 onclick = String.Empty;
158 if (HasAttributes && Attributes ["onclick"] != null) {
159 onclick = ClientScriptManager.EnsureEndsWithSemicolon (onclick + Attributes ["onclick"]);
160 Attributes.Remove ("onclick");
164 onclick += GetClientScriptEventReference ();
166 if (onclick.Length > 0)
167 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, onclick);
170 internal virtual string GetClientScriptEventReference ()
172 PostBackOptions options = GetPostBackOptions ();
175 if (options.PerformValidation || !String.IsNullOrEmpty (options.ActionUrl))
176 return page != null ? page.ClientScript.GetPostBackEventReference (options, true) : String.Empty;
179 page.ClientScript.RegisterForEventValidation (options);
184 protected virtual PostBackOptions GetPostBackOptions ()
186 PostBackOptions options = new PostBackOptions (this);
189 options.ActionUrl = (PostBackUrl.Length > 0 ?
190 (page != null ? page.ResolveClientUrl (PostBackUrl) : null)
193 options.Argument = String.Empty;
194 options.ClientSubmit = true;
195 options.RequiresJavaScriptProtocol = true;
196 options.PerformValidation = CausesValidation && page != null && page.AreValidatorsUplevel (ValidationGroup);
197 if (options.PerformValidation)
198 options.ValidationGroup = ValidationGroup;
200 options.ValidationGroup = null;
205 protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
208 string unique = UniqueID;
209 x = postCollection [unique + ".x"];
210 y = postCollection [unique + ".y"];
211 if (!String.IsNullOrEmpty (x) && !String.IsNullOrEmpty (y)) {
212 pos_x = Int32.Parse(x);
213 pos_y = Int32.Parse(y);
214 Page.RegisterRequiresRaiseEvent (this);
217 x = postCollection [unique];
218 if (!String.IsNullOrEmpty (x)) {
219 pos_x = Int32.Parse (x);
221 Page.RegisterRequiresRaiseEvent (this);
229 protected virtual void RaisePostDataChangedEvent ()
233 protected virtual void RaisePostBackEvent (string eventArgument)
235 ValidateEvent (UniqueID, String.Empty);
236 if (CausesValidation) {
239 page.Validate (ValidationGroup);
242 OnClick (new ImageClickEventArgs (pos_x, pos_y));
243 OnCommand (new CommandEventArgs (CommandName, CommandArgument));
246 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
248 return LoadPostData (postDataKey, postCollection);
252 void IPostBackDataHandler.RaisePostDataChangedEvent ()
254 RaisePostDataChangedEvent ();
257 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
259 RaisePostBackEvent (eventArgument);
262 protected virtual void OnClick (ImageClickEventArgs e)
264 if (Events != null) {
265 ImageClickEventHandler eh = (ImageClickEventHandler) (Events [ClickEvent]);
271 protected virtual void OnCommand (CommandEventArgs e)
273 if (Events != null) {
274 CommandEventHandler eh = (CommandEventHandler) (Events [CommandEvent]);
279 RaiseBubbleEvent (this, e);
282 protected internal override void OnPreRender (EventArgs e)
285 if (page != null && IsEnabled)
286 page.RegisterRequiresPostBack (this);
289 [WebSysDescription ("")]
290 [WebCategory ("Action")]
291 public event ImageClickEventHandler Click {
292 add { Events.AddHandler (ClickEvent, value); }
293 remove { Events.RemoveHandler (ClickEvent, value); }
296 [WebSysDescription ("")]
297 [WebCategory ("Action")]
298 public event CommandEventHandler Command {
299 add { Events.AddHandler (CommandEvent, value); }
300 remove { Events.RemoveHandler (CommandEvent, value); }
303 string IButtonControl.Text {
305 set { Text = value; }
308 event EventHandler IButtonControl.Click {
309 add { Events.AddHandler (ClickEvent, value); }
310 remove { Events.RemoveHandler (ClickEvent, value); }