da58ecc38bef55718e2f6d11fd347aeb610b580f
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / LinkButton.cs
1 //
2 // System.Web.UI.WebControls.LinkButton.cs
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@novell.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System.ComponentModel;
30 using System.Security.Permissions;
31
32 namespace System.Web.UI.WebControls {
33
34         // CAS
35         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
36         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37         // attributes
38         [ControlBuilder(typeof(LinkButtonControlBuilder))]
39         [DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
40         [DefaultEvent("Click")]
41         [DefaultProperty("Text")]
42         [Designer("System.Web.UI.Design.WebControls.LinkButtonDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
43         [ParseChildren(false)]
44 #if NET_2_0
45         [SupportsEventValidation]
46         [ToolboxData("<{0}:LinkButton runat=\"server\">LinkButton</{0}:LinkButton>")]
47 #else           
48         [ToolboxData("<{0}:LinkButton runat=server>LinkButton</{0}:LinkButton>")]
49 #endif          
50         public class LinkButton : WebControl, IPostBackEventHandler
51 #if NET_2_0
52         , IButtonControl
53 #endif
54         {
55         
56                 public LinkButton () : base (HtmlTextWriterTag.A) 
57                 {
58                 }
59         
60         
61                 protected override void AddAttributesToRender (HtmlTextWriter w)
62                 {
63                         if (Page != null)
64                                 Page.VerifyRenderingInServerForm (this);
65
66 #if NET_2_0
67                         string onclick = OnClientClick;
68                         onclick = ClientScriptManager.EnsureEndsWithSemicolon (onclick);
69                         if (Attributes ["onclick"] != null) {
70                                 onclick = ClientScriptManager.EnsureEndsWithSemicolon (onclick + Attributes ["onclick"]);
71                                 Attributes.Remove ("onclick");
72                         }
73
74                         if (onclick.Length > 0)
75                                 w.AddAttribute (HtmlTextWriterAttribute.Onclick, onclick);
76
77                         if (Enabled && Page != null) {
78                                 PostBackOptions options = GetPostBackOptions ();
79                                 string href = Page.ClientScript.GetPostBackEventReference (options, true);
80                                 w.AddAttribute (HtmlTextWriterAttribute.Href, href);
81                         }
82                         base.AddAttributesToRender (w);
83 #else
84                         base.AddAttributesToRender (w);
85                         if (Page == null || !Enabled)
86                                 return;
87                         
88                         if (CausesValidation && Page.AreValidatorsUplevel ()) {
89                                 ClientScriptManager csm = new ClientScriptManager (Page);
90                                 w.AddAttribute (HtmlTextWriterAttribute.Href,
91                                                 String.Format ("javascript:{{if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); {0};}}",
92                                                                csm.GetPostBackEventReference (this, String.Empty)));
93                         } else {
94                                 w.AddAttribute (HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackClientHyperlink (this, ""));
95                         }
96 #endif
97                 }
98
99 #if NET_2_0
100                 protected virtual 
101 #endif          
102                 void RaisePostBackEvent (string eventArgument)
103                 {
104                         if (CausesValidation)
105 #if NET_2_0
106                                 Page.Validate (ValidationGroup);
107 #else
108                                 Page.Validate ();
109 #endif
110                         
111                         OnClick (EventArgs.Empty);
112                         OnCommand (new CommandEventArgs (CommandName, CommandArgument));
113                 }
114                 
115                 void IPostBackEventHandler.RaisePostBackEvent (string ea)
116                 {
117                         RaisePostBackEvent (ea);
118                 }
119
120                 protected override void AddParsedSubObject (object obj)
121                 {
122                         if (HasControls ()) {
123                                 base.AddParsedSubObject (obj);
124                                 return;
125                         }
126                         
127                         LiteralControl lc = obj as LiteralControl;
128
129                         if (lc == null) {
130                                 string s = Text;
131                                 if (s.Length != 0) {
132                                         Text = null;
133                                         Controls.Add (new LiteralControl (s));
134                                 }
135                                 base.AddParsedSubObject (obj);
136                         } else {
137                                 Text = lc.Text;
138                         }
139                 }
140
141 #if NET_2_0
142                 protected virtual PostBackOptions GetPostBackOptions ()
143                 {
144                         PostBackOptions options = new PostBackOptions (this);
145                         options.ActionUrl = (PostBackUrl.Length > 0 ? Page.ResolveClientUrl (PostBackUrl) : null);
146                         options.ValidationGroup = null;
147                         options.Argument = "";
148                         options.ClientSubmit = true;
149                         options.RequiresJavaScriptProtocol = true;
150                         options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
151                         if (options.PerformValidation)
152                                 options.ValidationGroup = ValidationGroup;
153
154                         return options;
155                 }
156 #endif          
157
158                 protected override void LoadViewState (object savedState)
159                 {
160                         base.LoadViewState (savedState);
161
162                         // Make sure we clear child controls when this happens
163                         if (ViewState ["Text"] != null)
164                                 Text = (string) ViewState ["Text"];
165                 }
166
167                 [MonoTODO ("Why override?")]
168 #if NET_2_0
169                 protected internal
170 #else           
171                 protected
172 #endif          
173                 override void OnPreRender (EventArgs e)
174                 {
175                         base.OnPreRender (e);
176                 }
177         
178 #if NET_2_0
179                 protected internal
180 #else           
181                 protected
182 #endif          
183                 override void RenderContents (HtmlTextWriter writer)
184                 {
185                         if (HasControls ())
186                                 base.RenderContents (writer);
187                         else
188                                 writer.Write (Text);
189                 }
190         
191         
192 #if ONLY_1_1
193                 [Bindable(false)]
194 #endif          
195                 [DefaultValue(true)]
196                 [WebSysDescription ("")]
197                 [WebCategory ("Behavior")]
198 #if NET_2_0
199                 [Themeable (false)]
200                 public virtual
201 #else
202                 public
203 #endif          
204                 bool CausesValidation {
205                         get {
206                                 return ViewState.GetBool ("CausesValidation", true);
207                         }
208                         set {
209                                 ViewState ["CausesValidation"] = value;
210                         }
211                 
212                 }
213
214                 [Bindable(true)]
215                 [DefaultValue("")]
216                 [WebSysDescription ("")]
217                 [WebCategory ("Behavior")]
218 #if NET_2_0
219                 [Themeable (false)]
220 #endif          
221                 public string CommandArgument {
222                         get {
223                                 return ViewState.GetString ("CommandArgument", "");
224                         }
225                         set {
226                                 ViewState ["CommandArgument"] = value;
227                         }
228                 }
229
230                 [DefaultValue("")]
231                 [WebSysDescription ("")]
232                 [WebCategory ("Behavior")]
233 #if NET_2_0
234                 [Themeable (false)]
235 #endif          
236                 public string CommandName {
237                         get {
238                                 return ViewState.GetString ("CommandName", ""); 
239                         }
240                 
241                         set {
242                                 ViewState ["CommandName"] = value;
243                         }
244                 
245                 }
246
247 #if NET_2_0
248                 [DefaultValue ("")]
249                 [Themeable (false)]
250                 [WebSysDescription ("")]
251                 [WebCategoryAttribute ("Behavior")]
252                 public virtual string OnClientClick
253                 {
254                         get {
255                                 return ViewState.GetString ("OnClientClick", "");
256                         }
257                         set {
258                                 ViewState ["OnClientClick"] = value;
259                         }
260                 }
261
262 #endif          
263
264                 [Bindable(true)]
265                 [DefaultValue("")]
266                 [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
267 #if NET_2_0
268                 [Localizable (true)]
269 #endif
270                 [WebSysDescription ("")]
271                 [WebCategory ("Appearance")]
272                 public virtual string Text {
273                         get {
274                                 return ViewState.GetString ("Text", "");        
275                         }
276                         set {
277                                 ViewState ["Text"] = value;     
278                                 if (HasControls ())
279                                         Controls.Clear ();
280                         }
281                 }
282
283                 protected virtual void OnClick (EventArgs e)
284                 {
285                         EventHandler h = (EventHandler) Events [ClickEvent];
286                         if (h != null)
287                                 h (this, e);
288                 }
289                 static readonly object ClickEvent = new object ();
290
291                 [WebSysDescription ("")]
292                 [WebCategory ("Action")]
293                 public event EventHandler Click {
294                         add { Events.AddHandler (ClickEvent, value); }
295                         remove { Events.RemoveHandler (ClickEvent, value); }
296                 }
297
298                 protected virtual void OnCommand (CommandEventArgs e)
299                 {
300                         CommandEventHandler h = (CommandEventHandler) Events [CommandEvent];
301                         if (h != null)
302                                 h (this, e);
303
304                         RaiseBubbleEvent (this, e);
305                 }
306                 static readonly object CommandEvent = new object ();
307
308                 [WebSysDescription ("")]
309                 [WebCategory ("Action")]
310                 public event CommandEventHandler Command {
311                         add { Events.AddHandler (CommandEvent, value); }
312                         remove { Events.RemoveHandler (CommandEvent, value); }
313                 }
314 #if NET_2_0
315                 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
316                 [Themeable (false)]
317                 [UrlProperty ("*.aspx")]
318                 [DefaultValue ("")]
319                 public virtual string PostBackUrl {
320                         get {
321                                 return ViewState.GetString ("PostBackUrl", String.Empty);
322                         }
323                         set {
324                                 ViewState["PostBackUrl"] = value;
325                         }
326                 }
327
328                 [DefaultValue ("")]
329                 [Themeable (false)]
330                 [WebSysDescription ("")]
331                 [WebCategoryAttribute ("Behavior")]
332                 public virtual string ValidationGroup {
333                         get {
334                                 return ViewState.GetString ("ValidationGroup", "");     
335                         }
336                         set {
337                                 ViewState ["ValidationGroup"] = value;  
338                         }
339                 }       
340 #endif
341         }
342 }
343
344