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 using System.Collections;
\r
14 using System.Collections.Specialized;
\r
16 using System.Web.UI;
\r
17 using System.ComponentModel;
\r
19 namespace System.Web.UI.WebControls
\r
21 [DefaultEvent("Click")]
\r
22 public class ImageButton: Image, IPostBackDataHandler, IPostBackEventHandler
\r
24 private static readonly object ClickEvent = new object();
\r
25 private static readonly object CommandEvent = new object();
\r
29 public ImageButton(): base()
\r
33 [DefaultValue (true), Bindable (false), WebCategory ("Behavior")]
34 [WebSysDescription ("Determines if validation is performed when clicked.")]
\r
35 public bool CausesValidation
\r
39 object o = ViewState["CausesValidation"];
\r
46 ViewState["CausesValidation"] = value;
\r
50 [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
51 [WebSysDescription ("An argument for the Command of this control.")]
\r
52 public string CommandArgument
\r
56 object o = ViewState["CommandArgument"];
\r
59 return String.Empty;
\r
63 ViewState["CommandArgument"] = value;
\r
67 [DefaultValue (""), WebCategory ("Behavior")]
68 [WebSysDescription ("The name of the Command of this control.")]
\r
69 public string CommandName
\r
73 object o = ViewState["CommandName"];
\r
76 return String.Empty;
\r
80 ViewState["CommandName"] = value;
\r
85 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
86 protected override HtmlTextWriterTag TagKey
\r
90 return HtmlTextWriterTag.Input;
\r
94 [WebCategory ("Action")]
95 [WebSysDescription ("Raised when the LinkButton is clicked.")]
\r
96 public event ImageClickEventHandler Click
\r
100 Events.AddHandler(ClickEvent, value);
\r
104 Events.RemoveHandler(ClickEvent, value);
\r
108 [WebCategory ("Action")]
109 [WebSysDescription ("Raised when a LinkButton Command is executed.")]
\r
110 public event CommandEventHandler Command
\r
114 Events.AddHandler(CommandEvent, value);
\r
118 Events.RemoveHandler(CommandEvent, value);
\r
122 protected override void AddAttributesToRender(HtmlTextWriter writer)
\r
124 writer.AddAttribute(HtmlTextWriterAttribute.Type, "image");
\r
125 writer.AddAttribute(HtmlTextWriterAttribute.Name, UniqueID);
\r
126 if(Page != null && CausesValidation)
\r
128 if(Page.Validators.Count > 0)
\r
130 writer.AddAttribute(HtmlTextWriterAttribute.Onclick, Utils.GetClientValidatedEvent(Page));
\r
131 writer.AddAttribute("language", "javascript");
\r
134 base.AddAttributesToRender(writer);
\r
137 protected virtual void OnClick(ImageClickEventArgs e)
\r
141 ImageClickEventHandler iceh = (ImageClickEventHandler)(Events[ClickEvent]);
\r
147 protected virtual void OnCommand(CommandEventArgs e)
\r
151 CommandEventHandler ceh = (CommandEventHandler)(Events[CommandEvent]);
\r
154 RaiseBubbleEvent(this, e);
\r
158 protected override void OnPreRender(EventArgs e)
\r
162 Page.RegisterRequiresPostBack(this);
\r
166 bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
\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
173 x = Int32.Parse(xCoord);
\r
174 y = Int32.Parse(yCoord);
\r
175 Page.RegisterRequiresRaiseEvent(this);
176 } else if (id != null)
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.
183 x = Int32.Parse (id);
184 Page.RegisterRequiresRaiseEvent (this);
189 void IPostBackDataHandler.RaisePostDataChangedEvent()
\r
193 void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
\r
195 if(CausesValidation)
\r
197 OnClick(new ImageClickEventArgs(x, y));
\r
198 OnCommand(new CommandEventArgs(CommandName, CommandArgument));
\r