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
;
35 using System
.Collections
.Specialized
;
38 using System
.ComponentModel
;
40 namespace System
.Web
.UI
.WebControls
42 [DefaultEvent("Click")]
43 public class ImageButton
: Image
, IPostBackDataHandler
, IPostBackEventHandler
45 private static readonly object ClickEvent
= new object();
46 private static readonly object CommandEvent
= new object();
50 public ImageButton(): base()
54 [DefaultValue (true), Bindable (false), WebCategory ("Behavior")]
55 [WebSysDescription ("Determines if validation is performed when clicked.")]
56 public bool CausesValidation
60 object o
= ViewState
["CausesValidation"];
67 ViewState
["CausesValidation"] = value;
71 [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
72 [WebSysDescription ("An argument for the Command of this control.")]
73 public string CommandArgument
77 object o
= ViewState
["CommandArgument"];
84 ViewState
["CommandArgument"] = value;
88 [DefaultValue (""), WebCategory ("Behavior")]
89 [WebSysDescription ("The name of the Command of this control.")]
90 public string CommandName
94 object o
= ViewState
["CommandName"];
101 ViewState
["CommandName"] = value;
106 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Hidden
)]
107 protected override HtmlTextWriterTag TagKey
111 return HtmlTextWriterTag
.Input
;
115 [WebCategory ("Action")]
116 [WebSysDescription ("Raised when the LinkButton is clicked.")]
117 public event ImageClickEventHandler Click
121 Events
.AddHandler(ClickEvent
, value);
125 Events
.RemoveHandler(ClickEvent
, value);
129 [WebCategory ("Action")]
130 [WebSysDescription ("Raised when a LinkButton Command is executed.")]
131 public event CommandEventHandler Command
135 Events
.AddHandler(CommandEvent
, value);
139 Events
.RemoveHandler(CommandEvent
, value);
143 protected override void AddAttributesToRender(HtmlTextWriter writer
)
145 writer
.AddAttribute(HtmlTextWriterAttribute
.Type
, "image");
146 writer
.AddAttribute(HtmlTextWriterAttribute
.Name
, UniqueID
);
147 if(Page
!= null && CausesValidation
)
149 if(Page
.Validators
.Count
> 0)
151 writer
.AddAttribute(HtmlTextWriterAttribute
.Onclick
, Utils
.GetClientValidatedEvent(Page
));
152 writer
.AddAttribute("language", "javascript");
155 base.AddAttributesToRender(writer
);
158 protected virtual void OnClick(ImageClickEventArgs e
)
162 ImageClickEventHandler iceh
= (ImageClickEventHandler
)(Events
[ClickEvent
]);
168 protected virtual void OnCommand(CommandEventArgs e
)
172 CommandEventHandler ceh
= (CommandEventHandler
)(Events
[CommandEvent
]);
175 RaiseBubbleEvent(this, e
);
179 protected override void OnPreRender(EventArgs e
)
183 Page
.RegisterRequiresPostBack(this);
187 bool IPostBackDataHandler
.LoadPostData(string postDataKey
, NameValueCollection postCollection
)
189 string xCoord
= postCollection
[UniqueID
+ ".x"];
190 string yCoord
= postCollection
[UniqueID
+ ".y"];
191 string id
= postCollection
[UniqueID
];
192 if(xCoord
!= null && yCoord
!= null && xCoord
.Length
> 0 && yCoord
.Length
> 0)
194 x
= Int32
.Parse(xCoord
);
195 y
= Int32
.Parse(yCoord
);
196 Page
.RegisterRequiresRaiseEvent(this);
197 } else if (id
!= null)
200 // This is a workaround for bug #49819. It appears that the .x and .y
201 // values are not being posted, and only the x value is being posted
202 // with the ctrl's id as the key.
204 x
= Int32
.Parse (id
);
205 Page
.RegisterRequiresRaiseEvent (this);
210 void IPostBackDataHandler
.RaisePostDataChangedEvent()
214 void IPostBackEventHandler
.RaisePostBackEvent(string eventArgument
)
218 OnClick(new ImageClickEventArgs(x
, y
));
219 OnCommand(new CommandEventArgs(CommandName
, CommandArgument
));